C++ std::unordered_map reserve() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::unordered_map

Definition

In C++, std::unordered_map::reserve() prepares the table for about n elements.

It is the beginner-friendly way to tune an unordered_map: if you know you will insert many keys, reserve first. Include <unordered_map> to use it.

This is a member of std::unordered_map<Key, T>, a hash table of key and value pairs. Each key is unique. Find, insert, and erase are usually average constant time. Unlike std::map, keys are not sorted and iteration order is unspecified. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.

Syntax

Pass the number of elements you plan to store.

C++
m.reserve(1000);     // ready for about 1000 pairs
m.insert({"a", 1});
CallWhat it does
m.reserve(n)Ensure bucket_count is large enough for n elements under max_load_factor().

reserve(n) does not change size(). It only adjusts buckets. Unlike std::vector::reserve, there is no separate capacity() to read afterward; look at bucket_count() or load_factor().

Library declarations
C++
void reserve(size_type n);

Parameters

ParameterDescription
nExpected number of elements to accommodate.

Return Value

Return ValueDescription
voidNothing. The table may grow.

C++ Examples

Example 1 — reserve() Before a Loop

Reserve room, then insert, and print the final size.

C++
#include <iostream>
#include <string>
#include <unordered_map>

int main() {
    std::unordered_map<int, std::string> m;
    m.reserve(5);

    m[1] = "one";
    m[2] = "two";
    m[3] = "three";

    std::cout << "size: " << m.size() << '\n';
    std::cout << "buckets: " << m.bucket_count() << '\n';
    std::cout << "1 -> " << m.at(1) << '\n';
    return 0;
}

How It Works

  1. reserve(5) grows the table early for about five elements.
  2. Three inserts follow without needing as many growth steps.
  3. at(1) reads a stored value to show the map works normally after reserve.

Example 2 — Fewer Growth Steps With Reserve

Compare bucket counts when inserting with and without an early reserve.

C++
#include <iostream>
#include <unordered_map>

int main() {
    std::unordered_map<int, int> slow;
    std::unordered_map<int, int> fast;
    fast.reserve(100);

    for (int i = 0; i < 100; ++i) {
        slow[i] = i;
        fast[i] = i;
    }

    std::cout << "slow buckets: " << slow.bucket_count() << '\n';
    std::cout << "fast buckets: " << fast.bucket_count() << '\n';
    std::cout << "same size: " << std::boolalpha
              << (slow.size() == fast.size()) << '\n';
    return 0;
}

How It Works

  1. fast reserves for 100 elements up front. slow grows as needed.
  2. Both end with 100 pairs. Final bucket counts may look similar after automatic growth.
  3. The win for reserve is fewer rebuilds during the loop, which matters more for larger n or expensive keys.

Example 3 — reserve() Leaves size() Unchanged

Reserve a large number and show size stays zero until you insert.

C++
#include <iostream>
#include <unordered_map>

int main() {
    std::unordered_map<int, int> m;
    m.reserve(500);

    std::cout << "size: " << m.size() << '\n';
    std::cout << "empty: " << std::boolalpha << m.empty() << '\n';
    std::cout << "buckets: " << m.bucket_count() << '\n';
    return 0;
}

How It Works

  1. reserve(500) only prepares buckets.
  2. size() stays 0 and empty() is still true.
  3. bucket_count() has grown so later inserts have room.

Frequently Asked Questions

It ensures the table has enough buckets to hold about n elements under max_load_factor.
No. It does not insert elements.
reserve talks about elements. rehash talks about buckets. reserve is usually the clearer choice for beginners.
No. std::map is a tree and has no bucket table. reserve is an unordered_map feature.
Call it when you know an approximate final size. For tiny maps it does not matter much.

Next: size()

Keep going through the std::unordered_map functions. Count the key and value pairs in the unordered_map.

size() 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.

10 people found this page helpful