C++ unordered_multimap::rehash() Function



The C++ std::unordered_multimap::rehash() function is used to set 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. Rehashes are automatically performed by the container whenever its load factor is going to exceed its max_load_factor in an operation.

If n is greater than the current number of buckets in the container, a rehash is occurred , similarly if n is lower 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_multimap::rehash() function.

void rehash(size_type n);

Parameters

  • n − It indicates the requested number of buckets.

Return value

This function does not returns anything.

Example 1

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

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

Output

If we run the above code it will generate the following output −

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 the rehash() function.

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

Output

Following is the output of the above code −

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

Example 3

Let's look at the following example, where we are going to use the rehash() function to set the number of buckets in the container.

#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
   unordered_multimap<int, int> umMap;
   umMap = { {1, 100}, {2, 20}, {3, 30}, {2, 20}, {1, 10} };
   cout << "Size of container : " << umMap.size() << endl;
   cout << "Initial bucket count : " << umMap.bucket_count() << endl;
   
   umMap.rehash(10);
   cout << "Size of container : " << umMap.size() << endl;
   cout << "current bucket count is : " << umMap.bucket_count() << endl;
   for(unsigned int i = 0; i < umMap.bucket_count(); i++){
      cout<<"The bucket #"<<i <<endl;
   }
   return 0;
}

Output

Output of the above code is as follows −

Size of container : 5
Initial bucket count : 5
Size of container : 5
current bucket count is : 11
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
Advertisements