In C++, std::unordered_map<Key, T> is a container that stores key and value pairs in a hash table. You look up a value by its key. The keys are unique and are not kept sorted. Iteration order is unspecified.
Finding, inserting, and erasing a key are usually average constant time. Read a value with at() or find(), add pairs with insert() or emplace(), and call reserve() before many inserts. Include <unordered_map> to use it.
This page teaches what a std::unordered_map is. The Unordered Map Functions index lists every function guide in this series.
Why choose an unordered_map?
Choose an unordered_map when you need fast key lookup and do not need sorted keys, for example names to ages, words to counts, or ids to records. If you need sorted output or lower_bound-style searches, use std::map. If you only need a list of values by position, use a std::vector.
Syntax
C++ Syntax
Common ways to create and use a std::unordered_map:
C++
#include <iostream>
#include <string>
#include <unordered_map>
std::unordered_map<std::string, int> a; // empty
std::unordered_map<std::string, int> b = {{"b", 2}, {"a", 1}}; // order unspecified
b.reserve(10); // prepare buckets
b["c"] = 3; // add or update with []
b.insert({"d", 4}); // add only if the key is new
b.emplace("e", 5); // build the pair in place
int x = b.at("a"); // throws if missing
auto it = b.find("c"); // iterator, or b.end()
bool has = b.contains("z"); // false (C++20)
b.erase("d"); // remove by key
std::size_t n = b.size(); // number of pairs
Reference
std::unordered_map vs std::map vs std::vector
Here is a quick comparison of three containers that beginners often mix up:
Feature
std::unordered_map
std::map
std::vector
Stores
Key and value pairs
Key and value pairs
Values only
Order of elements
Unspecified (hash buckets)
Sorted by key
The order you added them
Find by key
Usually constant
Logarithmic
Linear (search by value)
Duplicate keys
Not allowed
Not allowed
Not applicable
Ordered search (lower_bound())
No
Yes
Only if you sort it
Key type needs
A hash and operator==
operator< or a comparator
Not applicable
Header
<unordered_map>
<map>
<vector>
Guide
When to Use find(), at(), [] or contains()
Several members read or test a key, but they react differently when the key is missing:
std::unordered_map<Key, T> is a hash table from <unordered_map>. It stores key and value pairs, keeps keys unique, and finds a key in average constant time. Iteration order is unspecified.
unordered_map uses hashing, is usually faster to find a key, and does not sort keys. map keeps keys sorted and finds in logarithmic time. Choose map when you need sorted output or lower_bound-style range searches.
find() returns an iterator and gives end() for a missing key. at() returns the value but throws std::out_of_range for a missing key. m[key] never fails, but it adds a missing key with a default value, which changes the map.
Call reserve(n) before you insert about n keys. It grows the bucket table once and avoids repeated rehashes during a big loop.
Open the C++ std::unordered_map Functions index. It lists every member function in this series, from insert() and find() to reserve(), rehash(), and hash_function().