Home C++ Unordered Sets Functions rehash() C++ std::unordered_set rehash() Function Definition In C++, std::unordered_set::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_set> to use it.
This is a member of std::unordered_set<T>, a hash table of unique values . Find, insert, and erase are usually average constant time. Unlike std::set, values are not sorted and iteration order is unspecified. Elements are const, so you cannot change a stored value through an iterator. There is no operator[] and no at(). Prefer find() or contains() to look up a value, and call reserve() before many inserts.
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_set>
int main() {
std::unordered_set<std::string> m = {"a", "b"};
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 unordered_set starts with a small table and two values. rehash(50) asks for at least 50 buckets. The library may choose a nearby larger size.The values 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_set>
int main() {
std::unordered_set<std::string> m = {"x", "y"};
// auto bad = m.begin(); // would be invalid after rehash
m.rehash(100);
auto ok = m.find("x");
std::cout << std::boolalpha;
std::cout << "found x after rehash: " << (ok != m.end()) << '\n';
std::cout << "buckets grew: " << (m.bucket_count() >= 100) << '\n';
return 0;
} found x after rehash: true
buckets grew: true How It Works An iterator taken before rehash() must not be used afterward. After the rebuild, find("x") gets a fresh, valid iterator. The value is still present, and the bucket count is at least the requested size. Example 3 — rehash(n) Counts Buckets; reserve Counts Elements Contrast the two ways to grow the table.
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> a;
std::unordered_set<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_set 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.
7 people found this page helpful
Helpful Share Copy link Suggestion