C++ unordered_map
In this page:
#include <unordered_map>
std::unordered_map<key_type, value_type> map_name;
map_name[key] = value;
map_name.at(key);
What is unordered_map?
std::unordered_map is an associative container storing key-value pairs with no guaranteed order, implemented as a hash table.
Average-case lookup, insertion, and deletion run in O(1) time, faster than map for most workloads that do not need sorted keys.
Example: What is unordered_map?
// Include std::cout and std::cin
#include <iostream>
#include <unordered_map>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::unordered_map<std::string, int> ages;
ages["Alex"] = 30;
// Print to the console with cout
std::cout << ages["Alex"] << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Accessing Elements
Like map, unordered_map supports operator[] and at() for access. Since there is no ordering, elements are retrieved purely by key, not by position.
Example: Accessing Elements
// Include std::cout and std::cin
#include <iostream>
#include <unordered_map>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::unordered_map<std::string, int> ages = {{"Alex", 30}};
// Print to the console with cout
std::cout << ages.at("Alex") << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Hashing & Performance
unordered_map hashes each key to determine its bucket. This gives average O(1) operations but worst-case O(n) if many keys collide. bucket_count() and load_factor() expose the internal hash table state.
Example: Hashing & Performance
// Include std::cout and std::cin
#include <iostream>
#include <unordered_map>
// Program execution starts in main()
int main() {
std::unordered_map<int, int> m;
m[1] = 10;
// Print to the console with cout
std::cout << "buckets=" << m.bucket_count() << " load_factor=" << m.load_factor() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Iterating (Unordered)
Iterating an unordered_map visits elements in an unspecified, implementation-defined order that can change between insertions. Never rely on iteration order for correctness.
Example: Iterating (Unordered)
// Include std::cout and std::cin
#include <iostream>
#include <unordered_map>
// Program execution starts in main()
int main() {
std::unordered_map<int, int> m = {{1, 10}, {2, 20}};
for (const auto &[key, value] : m) {
// Print to the console with cout
std::cout << key << ":" << value << " ";
}
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Searching and Erasing
find(), count(), and erase() work the same way as std::map, but rely on hashing rather than tree traversal, making them faster on average for large unsorted datasets.
Example: Searching and Erasing
// Include std::cout and std::cin
#include <iostream>
#include <unordered_map>
// Program execution starts in main()
int main() {
std::unordered_map<int, int> m = {{1, 10}};
// Declare it and set it to m.find(1)
auto it = m.find(1);
if (it != m.end()) std::cout << "Found: " << it->second << std::endl;
m.erase(1);
// Print to the console with cout
std::cout << m.size() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Expecting
unordered_mapto iterate in sorted or insertion order. - Using
m[key]to test for existence, which inserts a default entry. - Using a custom key type without providing a hash function and equality.
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: