C++ std::unordered_map Functions

Beginner
8 min read
Updated: Nov 2026
34 function guides
Function index

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 hash table of unique keys.

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:

C++
#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 functions

Ask how many key and value pairs an unordered_map holds.

FunctionDescriptionTutorial
empty()Check whether the unordered_map has no elements.Try it
size()Count the key and value pairs in the unordered_map.Try it
max_size()Get the largest number of pairs the unordered_map could ever hold.Try it

Element access

1 function

Read a value by its key. at() is the safe way. The [] operator also exists, but it adds a missing key.

FunctionDescriptionTutorial
at()Read or change the value of a key. Throws if the key is missing.Try it

Modifiers

11 functions

Add, update, move, or remove pairs. Keys stay unique. Unlike std::map, pairs are not kept in sorted order.

FunctionDescriptionTutorial
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 functions

Search by key in average constant time. There is no lower_bound/upper_bound because keys are not sorted.

FunctionDescriptionTutorial
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 functions

Inspect and tune the hash table buckets, load factor, rehash, and reserve.

FunctionDescriptionTutorial
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 functions

Read the hash function, key equality predicate, and allocator.

FunctionDescriptionTutorial
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 functions

Walk through the pairs in an unspecified order. The c versions are read-only. unordered_map has no reverse iterators.

FunctionDescriptionTutorial
begin()Get an iterator to the first pair in iteration order.Try it
end()Get the iterator just past the last pair.Try it
cbegin()Get a read-only iterator to the first pair (C++11).Try it
cend()Get a read-only iterator past the last pair (C++11).Try it

Frequently Asked Questions

They are the functions you call on a std::unordered_map object with a dot, such as m.insert({"a", 1}), m.find("a"), m.erase("a"), or m.reserve(100). They need #include <unordered_map>.
This tutorial series covers 34: capacity, element access (at), modifiers, lookup, the bucket interface (bucket_count, rehash, reserve, load_factor, ...), hash observers (hash_function, key_eq, get_allocator), and forward iterators (begin/end, cbegin/cend).
The [] operator is covered inside the at() and insert() tutorials. Remember that m[key] adds the key with a default value when it is missing, while at() throws and find() returns end().
emplace() and emplace_hint() are C++11. insert_or_assign(), try_emplace(), extract(), and merge() are C++17. contains() is C++20. insert_range() is C++23.
Read the C++ std::unordered_map page first. Then open insert(), find(), at(), reserve(), and erase() from the tables below.

Start with insert()

Add a key and value pair to a map, the first thing most programs do.

insert() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful