C++ Unordered_set::rehash() function



The C++ std::unordered_set::rehash() function is used to set the number of buckets in the unordered_set 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, then the rehash is occurred.
  • 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_set::rehash() function.

void rehash ( size_type n );

Parameters

  • n − It indicates the minimum number of buckets.

Return Value

This function does not returns anything.

Example 1

Let's look at the following example, where we are going to demonstrate the usage of unordered_set::rehash() function.

#include 
#include 
#include 

int main () {
   std::unordered_set myset;

   myset.rehash(12);

   myset.insert("android");
   myset.insert("java");
   myset.insert("html");
   myset.insert("css");
   myset.insert("javascript");

   std::cout << "current bucket_count: " << myset.bucket_count() << std::endl;

   return 0;
}

Output

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

current bucket_count: 13

Example 2

Consider the following example, where we are going to consider the empty unordered_set which is empty and displaying its initial bucket count and current bucket count after the use of the unordered_set::rehash() function.

#include 
#include 
#include 
using namespace std;

int main () {
   unordered_set uSet;
   cout<<"Initial bucket count: "<

Output

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

Initial bucket count: 1
current bucket_count: 11

Example 3

Let's look at the another example of the usage of the unordered_set:: rehash() function and displaying the count before and after the use of function.

#include 
#include 
#include 
using namespace std;

int main () {
   unordered_set uSet = {1, 2, 3, 4, 5};
   cout<<"Initial bucket count: "<

Output

Following is the output of the above code −

Initial bucket count: 13
current bucket_count: 17

Example 4

Following is the example, where we are going to use the unordered_set::rehash() function to set the number of buckets and also display the number of buckets.

#include 
#include 
using namespace std;

int main() {
   unordered_set uSet;
   uSet = { 2, 4, 6, 8 };
   cout << "Size of unorder_set container : " << uSet.size() << endl;
   cout << "Initial bucket count : " << uSet.bucket_count() << endl;
   
   uSet.rehash(12);
   cout << "Size of unorder_set container : " << uSet.size() << endl;
   cout << "current bucket count is : " << uSet.bucket_count() << endl;
   for(unsigned int i = 0; i < uSet.bucket_count(); i++){
      cout<<"The bucket #"<

Output

Output of the above code is as follows −

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