Home C++ Unordered Maps Functions rehash() C++ std::unordered_map rehash() Function 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.
m.rehash(64); // at least 64 buckets
std::cout << m.bucket_count(); Call What 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 void rehash(size_type n); Parameters Parameter Description nRequested minimum bucket count.
Return Value Return Value Description voidNothing. The table is rebuilt.
C++ Examples 1. Request more buckets 2. Iterators do not survive 3. rehash vs reserve Example 1 — rehash() Raises Bucket Count Call rehash and print the new bucket count.
#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;
} before: 13
after: 53
size: 2 How It Works The map starts with a small table and two pairs. rehash(50) asks for at least 50 buckets. The library may choose a nearby larger size.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.
#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 An iterator taken before rehash() must not be used afterward. After the rebuild, find("x") gets a fresh, valid iterator. 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.
#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;
} after rehash(20): 23
after reserve(20): 23 How It Works rehash(20) talks about buckets directly.reserve(20) talks about elements; it chooses buckets from that count and max_load_factor().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 What does rehash(n) do? It rebuilds the hash table so there are at least n buckets (and enough for the current size under max_load_factor).
Does rehash change size()? No. It rearranges the same elements.
Are iterators valid after rehash? No. They are invalidated. Look up keys again with find().
When should I use rehash instead of reserve? Use rehash when you care about the exact bucket count. Use reserve when you know how many elements you will insert.
Can rehash shrink the table? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion