C++ std::unordered_map rehash() Function

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

Definition

In C++, std::unordered_map::rehash() rebuilds the hash table so it has at least n buckets.

Every key is placed into a bucket again. Use it when you want direct control of the table size. 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 minimum number of buckets you want.

C++
m.rehash(64);                 // at least 64 buckets
std::cout << m.bucket_count();
CallWhat it does
m.rehash(n)Make the bucket count at least n (and large enough for the current size / max_load_factor).

If n is smaller than what the current elements need under max_load_factor(), the library still keeps enough buckets for that limit. rehash(0) can shrink toward a fitting size.

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

Parameters

ParameterDescription
nRequested minimum bucket count.

Return Value

Return ValueDescription
voidNothing. The table is rebuilt.

C++ Examples

Example 1 — rehash() Raises Bucket Count

Call rehash and print the new bucket count.

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

int main() {
    std::unordered_map<std::string, int> m = {{"a", 1}, {"b", 2}};

    std::cout << "before: " << m.bucket_count() << '\n';
    m.rehash(50);
    std::cout << "after: " << m.bucket_count() << '\n';
    std::cout << "size: " << m.size() << '\n';
    return 0;
}

How It Works

  1. The map starts with a small table and two pairs.
  2. rehash(50) asks for at least 50 buckets. The library may choose a nearby larger size.
  3. The pairs are still there: size() stays 2.

Example 2 — Do Not Keep Iterators Across Rehash

Show that you must not use an old iterator after rehash.

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

int main() {
    std::unordered_map<std::string, int> m = {{"x", 1}, {"y", 2}};

    // auto bad = m.begin();  // would be invalid after rehash
    m.rehash(100);

    auto ok = m.find("x");
    if (ok != m.end()) {
        std::cout << ok->first << "=" << ok->second << '\n';
    }
    std::cout << "buckets: " << m.bucket_count() << '\n';
    return 0;
}

How It Works

  1. An iterator taken before rehash() must not be used afterward.
  2. After the rebuild, find("x") gets a fresh, valid iterator.
  3. The key is still present, and the bucket count has grown.

Example 3 — rehash(n) Counts Buckets; reserve Counts Elements

Contrast the two ways to grow the table.

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

int main() {
    std::unordered_map<int, int> a;
    std::unordered_map<int, int> b;

    a.rehash(20);     // want >= 20 buckets
    b.reserve(20);    // prepare for ~20 elements

    std::cout << "after rehash(20): " << a.bucket_count() << '\n';
    std::cout << "after reserve(20): " << b.bucket_count() << '\n';
    return 0;
}

How It Works

  1. rehash(20) talks about buckets directly.
  2. reserve(20) talks about elements; it chooses buckets from that count and max_load_factor().
  3. With a default max load factor of 1, both often land near the same bucket count, but the intent of the call is different.

Frequently Asked Questions

It rebuilds the hash table so there are at least n buckets (and enough for the current size under max_load_factor).
No. It rearranges the same elements.
No. They are invalidated. Look up keys again with find().
Use rehash when you care about the exact bucket count. Use reserve when you know how many elements you will insert.
Yes, if you pass a small n and the current size still fits under max_load_factor.

Next: reserve()

Keep going through the std::unordered_map functions. Prepare buckets for about n elements (avoids rehashes).

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

9 people found this page helpful