C++ std::unordered_map Functions

About This Index
This page is the C++ std::unordered_map Functions index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs.
Learn what a std::unordered_map is on /cpp/map. Use this page when you need a specific member function such as insert(), find(), or reserve().
A std::unordered_map<Key, T> stores key and value pairs with hashing, allows each key only once, and does not keep keys sorted. Reading with m[key] adds a missing key, so prefer at() or find() to read. Call reserve() before many inserts. The 34 functions are listed below.
How to Call an Unordered Map Function
Include <unordered_map>, create the map, and call members with a dot:
#include <iostream>
#include <unordered_map>
#include <string>
std::map<std::string, int> m = {{"b", 2}, {"a", 1}};
m.insert({"c", 3}); // a=1 b=2 c=3
m.insert_or_assign("a", 10); // a=10 (C++17)
int v = m.at("b"); // 2 (throws if missing)
auto it = m.find("c"); // iterator, or m.end()
bool has = m.contains("z"); // false (C++20)
m.erase("b"); // a=10 c=3
std::size_t n = m.size(); // 2 C++ std::unordered_map Functions Table
All function guides in this C++ tutorial series (34 tutorials), grouped by purpose.
Capacity
3 functionsAsk how many key and value pairs an unordered_map holds.
Element access
1 functionRead a value by its key. at() is the safe way. The [] operator also exists, but it adds a missing key.
Modifiers
11 functionsAdd, update, move, or remove pairs. Keys stay unique. Unlike std::map, pairs are not kept in sorted order.
| Function | Description | Tutorial |
|---|---|---|
clear() | Remove every pair from the unordered_map. | Try it |
insert() | Add a new key and value pair. An existing key is left alone. | Try it |
insert_range() | Insert every pair from a range in one call (C++23). | Try it |
insert_or_assign() | Insert a new key, or update the value if the key exists (C++17). | Try it |
emplace() | Build a new pair in place from its constructor arguments (C++11). | Try it |
emplace_hint() | Build a pair in place, using a hint for where it should go (C++11). | Try it |
try_emplace() | Add a pair only if the key is new, without touching your arguments (C++17). | Try it |
erase() | Remove a pair by key, by iterator, or a whole range. | Try it |
extract() | Take a pair out of the unordered_map as a node handle (C++17). | Try it |
merge() | Move pairs with new keys from another map into this one (C++17). | Try it |
swap() | Exchange the whole content of two unordered_maps quickly. | Try it |
Lookup
4 functionsSearch by key in average constant time. There is no lower_bound/upper_bound because keys are not sorted.
| Function | Description | Tutorial |
|---|---|---|
count() | Tell whether a key exists: the answer is 0 or 1 for an unordered_map. | Try it |
find() | Search for a key and get an iterator to its pair, or end(). | Try it |
contains() | Return true if the key is in the unordered_map (C++20). | Try it |
equal_range() | Get the range of pairs whose key equals the given key. | Try it |
Bucket interface
8 functionsInspect and tune the hash table buckets, load factor, rehash, and reserve.
| Function | Description | Tutorial |
|---|---|---|
bucket_count() | How many hash buckets the unordered_map currently has. | Try it |
max_bucket_count() | The largest number of buckets this unordered_map type could have. | Try it |
bucket_size() | How many elements sit in one hash bucket. | Try it |
bucket() | Which bucket a key would use (or already uses). | Try it |
load_factor() | Average number of elements per bucket right now. | Try it |
max_load_factor() | Get or set the load factor limit that triggers growth. | Try it |
rehash() | Rebuild the table with at least n buckets. | Try it |
reserve() | Prepare buckets for about n elements (avoids rehashes). | Try it |
Hash policy / observers
3 functionsRead the hash function, key equality predicate, and allocator.
| Function | Description | Tutorial |
|---|---|---|
hash_function() | Get the hash function object the unordered_map uses on keys. | Try it |
key_eq() | Get the key equality predicate the unordered_map uses. | Try it |
get_allocator() | Get a copy of the allocator the unordered_map uses for its nodes. | Try it |
Iterators
4 functionsWalk through the pairs in an unspecified order. The c versions are read-only. unordered_map has no reverse iterators.
Frequently Asked Questions
Start with insert()
Add a key and value pair to a map, the first thing most programs do.
12 people found this page helpful
