C++ Unordered_multimap Library - max_load_factor() Function


Advertisements

Description

The C++ function std::unordered_multimap::max_load_factor() assigns new load factor for the unordered_multimap container.

The load factor is calculated as follows −

load_factor = umm.size() / umm.bucket_count();

Default value of max_load_factor is 1.0

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.

Declaration

Following is the declaration for std::unordered_multimap::max_load_factor() function form std::unordered_map() header.

C++11

void max_load_factor(float z);

Parameters

z − The new maximum load factor.

Return value

None

Time complexity

Constant i.e. O(1)

Example

The following example shows the usage of std::unordered_multimap::max_load_factor() function.

#include <iostream>
#include <unordered_map>

using namespace std;

int main(void) {
   unordered_multimap<char, int> umm;

   cout << "Initial max_load_factor = " <<umm.max_load_factor()
        << endl;

   umm.max_load_factor(2);

   cout << "max_load_factor after set operation = "
        << umm.max_load_factor() << endl;

   return 0;
}

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

Initial max_load_factor = 1
max_load_factor after set operation = 2
unordered_map.htm
Advertisements