C++ Unordered_map::rehash() Function



The C++ std::unordered_map::rehash() function is used to sets the number of buckets in the container to n or more. A rehash is the reconstruction of the hash table; all its elements are rearranged into the new set of buckets according to their new hash value.

If n is greater than the current number of buckets in the container, a rehash is occurred, similarly if n is less than the current number of buckets in the container, the function may have no effect on the bucket count and may not force a rehash.

Syntax

Following is the syntax of std::unordered_map::rehash() function.

void rehash(size_type n);

Parameters

  • n − It inidicates the new number of buckets.

Return value

This function does not returns anything.

Example 1

In the following example, let's see the usage of rehash() function.

#include <iostream>
#include <unordered_map>
using namespace std;
int main (void) {
   unordered_map<char, int> uMap;
   cout << "Initial bucket_count: " << uMap.bucket_count() << endl;
   uMap.rehash(20);
   cout << "Current bucket_count: " << uMap.bucket_count() << endl;
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

Initial bucket_count: 1
Current bucket_count: 23

Example 2

Consider the following example, where we are going to count the size and bucket count before and after the use of rehash() function.

#include <iostream>
#include <unordered_map>
using namespace std;
int main (void) {
   unordered_map<int, int> uMap={{3, 30}, {2, 20}, {1, 10}};
   cout << "Size of container before use of rehash: "<<uMap.size() << endl;
   cout << "Initial bucket_count: " << uMap.bucket_count() << endl;
   uMap.rehash(22);
   cout << "Size of container after use of rehash: "<<uMap.size() << endl;
   cout << "Current bucket_count: " << uMap.bucket_count() << endl;
   return 0;
}

Output

Following is the output of the above code −

Size of container before use of rehash: 3
Initial bucket_count: 13
Size of container after use of rehash: 3
Current bucket_count: 23

Example 3

Let's look at the following example, where we are going to store the keys/values pair of integral types and using the rehash() function to set the number of buckets in the container.

#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
   unordered_map<int, int> uMap;
   uMap = { { 2, 2 }, { 3, 4 }, { 4, 6 }, { 5, 8 } };   
   cout << "Size of container : " << uMap.size() << endl;
   cout << "Initial bucket count : " << uMap.bucket_count() << endl;
   
   uMap.rehash(15);
   cout << "Size of container : " << uMap.size() << endl;
   cout << "current bucket count is : " << uMap.bucket_count() << endl;
   for(unsigned int i = 0; i < uMap.bucket_count(); i++){
      cout<<"The bucket #"<<i <<endl;
   }
   return 0;
}

Output

Output of the above code is as follows −

Size of container : 4
Initial bucket count : 5
Size of container : 4
current bucket count is : 17
The bucket #0
The bucket #1
The bucket #2
The bucket #3
The bucket #4
The bucket #5
The bucket #6
The bucket #7
The bucket #8
The bucket #9
The bucket #10
The bucket #11
The bucket #12
The bucket #13
The bucket #14
The bucket #15
The bucket #16
Advertisements