C++ Unordered_set::max_load_factor() Function



The C++ std::unordered_set::max_load_factor() function is used to get and set the maximum load factor in the unordered_set container. It automatically increase the number of buckets if the load factor exceeds this threshold, the default value of max_load_factor is 1.

The load factor influences the probability of collision in the hash table. The container uses the value of max_load_factor as the threshold that forces an increase in the number of buckets and thus causing a rehash.

This function has 2 polymorphic variants: the first return the maximum load facctor and the second one sets a new load factor (you can find the syntaxes of all the variants below).

Syntax

Following is the syntax of std::unordered_set::max_load_factor() function.

float max_load_factor() const;
or
void max_load_factor ( float new_size );

Parameters

  • new_size − It indicates the maximum load factor.

Return Value

This function returns the current load factor.

Example 1

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

#include <iostream>
#include <string>
#include <unordered_set>

int main () {
   std::unordered_set<std::string> myset =
      {"sai", "Ram", "krishna", "prasad", "Bangalore", "india"};

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

   float z = myset.max_load_factor();
   myset.max_load_factor ( z / 2.0 );
   std::cout << "[max_load_factor halved]" << std::endl;

   std::cout << "new max_load_factor: " << myset.max_load_factor() << std::endl;
   std::cout << "new size: " << myset.size() << std::endl;
   std::cout << "new bucket_count: " << myset.bucket_count() << std::endl;
   std::cout << "new load_factor: " << myset.load_factor() << std::endl;

   return 0;
}

Output

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

current max_load_factor: 1
current size: 6
current bucket_count: 13
current load_factor: 0.461538
[max_load_factor halved]
new max_load_factor: 0.5
new size: 6
new bucket_count: 13
new load_factor: 0.461538

Example 2

In the following example, we are going to use the unordered_set::max_load_factor() function to get the max_load_factor of the current unordered_set.

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;

int main () {
   unordered_set<string> uSet;
   unsigned max_load_factor = uSet.max_load_factor();
   cout<<"maximum load factor of empty set: "<<max_load_factor;
   return 0;
}

Output

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

maximum load factor of empty set: 1

Example 3

Consider the following example, where we are going to get the max_load_factor of the unordered_set and then we set the size inside the max_load_factor() function to check whether the max load factor is different or same after setting the size.

#include <iostream>
#include <unordered_set>
using namespace std;

int main(void) {
   unordered_set<char> us={'A', 'B', 'c', 'D'};
   cout << "Default max_load_factor of unordered_set = "<< us.max_load_factor() << endl;
   //assigning the size.
   us.max_load_factor(5.0/2.0);
   cout<<"max_load_factor after setting the size "<<endl;
   cout<<"current max_load_factor "<< us.max_load_factor()<<endl;
   return 0;
}

Output

Following is the output of the above code −

default max_load_factor of unordered_set = 1
max_load_factor after setting the size 
current max_load_factor 2.5

Example 4

Following is the example, where we are going to get the max_load_factor and load_factor for the current unordered_set.

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;

int main () {
   unordered_set<int> uSet = {1, 2, 3, 4, 5};
   float load_factor = uSet.load_factor();
   int max_load_factor = uSet.max_load_factor();
   cout<<"current load factor: "<< load_factor<<endl;
   cout<<"current max load factor: "<<max_load_factor<<endl;
   return 0;
}

Output

Output of the above code is as follows −

current load factor: 0.384615
current max load factor: 1
Advertisements