Unordered Map and Set in C++
Unordered Map and Unordered Set in C++.
IntermediateSTL Containers ProgramsExample 6 of 10
unordered-map-and-set.cpp
1#include <iostream>2#include <unordered_map>3#include <unordered_set>4#include <string>5using namespace std;67int main() {8 // Unordered Map9 unordered_map<string, int> wordCount;1011 wordCount["hello"] = 3;12 wordCount["world"] = 2;13 wordCount["c++"] = 5;14 wordCount["programming"] = 1;1516 cout << "Unordered Map (Hash Map):" << endl;17 for (const auto& pair : wordCount) {18 cout << pair.first << ": " << pair.second << endl;19 }2021 // Access22 cout << "\nCount of 'c++': " << wordCount["c++"] << endl;2324 // Check if key exists25 if (wordCount.find("hello") != wordCount.end()) {26 cout << "'hello' found" << endl;27 }2829 // Unordered Set30 unordered_set<int> numbers;3132 numbers.insert(5);33 numbers.insert(2);34 numbers.insert(8);35 numbers.insert(1);36 numbers.insert(5); // Duplicate ignored3738 cout << "\nUnordered Set (Hash Set): ";39 for (int num : numbers) {40 cout << num << " ";41 }42 cout << endl;4344 // Check if element exists45 if (numbers.find(8) != numbers.end()) {46 cout << "Element 8 found" << endl;47 }4849 // Size50 cout << "Set size: " << numbers.size() << endl;5152 // Bucket information (hash table details)53 cout << "\nHash table info:" << endl;54 cout << "Number of buckets: " << numbers.bucket_count() << endl;55 cout << "Load factor: " << numbers.load_factor() << endl;5657 return 0;58}
Output
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
What's going on
The program demonstrates unordered containers:
Unordered Map: key-value pairs with hash table
Unordered Set: unique elements with hash table
Fast insert, find, and erase operations
Hash table information (buckets, load factor)
Unordered containers provide O(1) average case performance.
Header Files Used
1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <unordered_map>
Provides unordered_map container class.
3.#include <unordered_set>
Provides unordered_set container class.
4.#include <string>
Provides string class for string keys.
Understanding Unordered Containers
Hash Table Concept
:
Uses hash function to map keys to buckets
O(1) average case operations
No automatic sorting
Faster than ordered containers for lookups
Key Features
:
Fast access (O(1) average)
No sorted order
Hash-based storage
Collision handling
Unordered Map
Declaration
:
unordered_map<string, int> wordCount;
Inserting
:
wordCount["hello"] = 3;
wordCount["world"] = 2;
How it works
:
Key-value pairs stored in hash table
Fast lookup by key
No sorted order
O(1) average insert/find
Accessing Unordered Map
Using [] Operator
:
int count = wordCount["c++"];
Using find()
:
auto it = wordCount.find("hello");
if (it != wordCount.end()) {
}
1// Key found
How it works
:
[] operator: creates if missing, returns value
find(): returns iterator, end() if not found
O(1) average case
Fast key-based access
Unordered Set
Declaration
:
unordered_set<int> numbers;
Inserting
:
numbers.insert(5);
numbers.insert(2);
numbers.insert(5); // Duplicate ignored
How it works
:
Stores unique elements
No sorted order
Fast membership testing
O(1) average insert/find
Hash Table Information
Bucket Information
:
numbers.bucket_count() // Number of buckets
numbers.load_factor() // Average elements per bucket
How it works
:
Buckets: hash table slots
Load factor: elements/buckets ratio
Lower load factor = better performance
Rehashing occurs when load factor high
When to Use Unordered Containers
Best For
:
Fast lookups (order doesn't matter)
Hash-based operations
When sorting not needed
Performance-critical lookups
Dictionary-like structures
Example Scenarios
:
Word frequency counting
Fast membership testing
Cache implementations
Symbol tables
Fast key-value lookups
Important Considerations
Performance
:
Average case: O(1)
Worst case: O(n) (hash collisions)
Faster than map/set for lookups
Slower for range queries
No Sorting
:
Elements not sorted
Cannot iterate in sorted order
Use map/set if sorting needed
Trade-off: speed vs order
Hash Collisions
:
Multiple keys map to same bucket
Handled automatically
Can degrade to O(n) worst case
Good hash function important
return 0;
This ends the program successfully.