Unordered Map and Set
Unordered Map and Unordered Set in C++
C++ Unordered Map and Set Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <string>
using namespace std;
int main() {
// Unordered Map
unordered_map<string, int> wordCount;
wordCount["hello"] = 3;
wordCount["world"] = 2;
wordCount["c++"] = 5;
wordCount["programming"] = 1;
cout << "Unordered Map (Hash Map):" << endl;
for (const auto& pair : wordCount) {
cout << pair.first << ": " << pair.second << endl;
}
// Access
cout << "\nCount of 'c++': " << wordCount["c++"] << endl;
// Check if key exists
if (wordCount.find("hello") != wordCount.end()) {
cout << "'hello' found" << endl;
}
// Unordered Set
unordered_set<int> numbers;
numbers.insert(5);
numbers.insert(2);
numbers.insert(8);
numbers.insert(1);
numbers.insert(5); // Duplicate ignored
cout << "\nUnordered Set (Hash Set): ";
for (int num : numbers) {
cout << num << " ";
}
cout << endl;
// Check if element exists
if (numbers.find(8) != numbers.end()) {
cout << "Element 8 found" << endl;
}
// Size
cout << "Set size: " << numbers.size() << endl;
// Bucket information (hash table details)
cout << "\nHash table info:" << endl;
cout << "Number of buckets: " << numbers.bucket_count() << endl;
cout << "Load factor: " << numbers.load_factor() << endl;
return 0;
}Unordered Map (Hash Map): programming: 1 c++: 5 world: 2 hello: 3 Count of 'c++': 5 'hello' found Unordered Set (Hash Set): 1 8 2 5 Element 8 found Set size: 4 Hash table info: Number of buckets: 8 Load factor: 0.5
Understanding Unordered Map and Set
This program teaches you how to use Unordered Map and Unordered Set in C++. These containers use hash tables for O(1) average case operations, making them faster than ordered map/set when sorting is not required. They don't maintain sorted order but provide extremely fast lookups.
---
1. What This Program Does
The program demonstrates unordered containers:
Unordered containers provide O(1) average case performance.
---
2. Header Files Used
---
3. Understanding Unordered Containers
Hash Table Concept
:
Key Features
:
---
4. Unordered Map
Declaration
:
unordered_map<string, int> wordCount;
Inserting
:
wordCount["hello"] = 3;
wordCount["world"] = 2;
How it works
:
---
5. Accessing Unordered Map
Using [] Operator
:
int count = wordCount["c++"];
Using find()
:
auto it = wordCount.find("hello");
if (it != wordCount.end()) {
}
// Key foundHow it works
:
---
6. Unordered Set
Declaration
:
unordered_set<int> numbers;
Inserting
:
numbers.insert(5);
numbers.insert(2);
numbers.insert(5); // Duplicate ignored
How it works
:
---
7. Hash Table Information
Bucket Information
:
numbers.bucket_count() // Number of buckets
numbers.load_factor() // Average elements per bucket
How it works
:
---
8. When to Use Unordered Containers
Best For
:
Example Scenarios
:
---
9. Important Considerations
Performance
:
No Sorting
:
Hash Collisions
:
---
10. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for learning hash-based containers, understanding performance trade-offs, and preparing for high-performance data structures in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Unordered Map and Set
This C++ program is part of the "STL Containers Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.