Unordered Multimap Max Load Factor Function In C++

The unorderedmultimap::loadfactor function is a C++ STL built-in function that returns the current value of the load factor in the unorderedmultimap container. The load factor is defined as the ratio of the total amount of components in a container (its size) to the total number of buckets (bucketcount):

loadfactor = bucketcount / size

The load factor impacts the probability of a collision inside a hash table (i.e., the probability of two entries being in the same bucket). By inducing a rehash each time an expansion is required, the container dynamically raises the number of buckets to maintain the load factor below a certain threshold (its maxloadfactor).

The unorderedmultimap::maxloadfactor function is a standard C++ STL function that produces the maximum load factor of the unorderedmultimap containers. This function also allows you to specify the maximum load factor.

Syntax:

It has the following function:

Example

unordered_multimap_name.max_load_factor().

Functions: The function accepts no parameters.

Return Value: It provides an integral value indicating the container's maximum load factor.

Example:

Example

// Program to implement 
// unordered_multimap::max_load_factor()
#include <bits/stdc++.h>
using namespace std;

int main()
{

	// main method
	unordered_multimap<int, int> ex;

	//inserting the key value elements
	ex.insert({ 5, 500 });
	ex.insert({ 6, 600 });

	// display of maximum load factor
	cout << "The max load factor value is: "
		<< ex.max_load_factor();

	cout << "\nThe key values of ex are:";
	for (auto ite = ex.begin(); ite != ex.end(); ite++) {
		cout << "{" << ite->first << ", " << ite->second << "} ";
	}

	return 0;
}

Output:

Output

The max load factor value is: 1
The key values of ex are:{6, 600} {5, 500}

Syntax:

Example

unordered_multimap_name.max_load_factor(N)

The function receives a single necessary parameter, N, that indicates the load factor to be set. This N represents the container's maximum load factor.

Return Value: Nothing is returned by the function

Example:

Example

// C++ Program to implement 
// unordered_multimap::max_load_factor(N)

#include <bits/stdc++.h>
using namespace std;

int main()
{

	//variable declaration
	unordered_multimap<int, int> ex1;

	// inserting the keys
	ex1.insert({ 10, 300 });
	ex1.insert({ 40, 400 });

	cout << "The value of the load balancer: "
		<<ex1.max_load_factor();

	// sets the load factor
	ex1.max_load_factor(300);

	cout << "\nThe max load factor of ex1 after setting it: "
		<< ex1.max_load_factor();

	cout << "\nThe elements of the sample ";
	for (auto ite = ex1.begin(); ite != ex1.end(); ite++) {
		cout << "{" << ite->first << ", " << ite->second << "} ";
	}

	return 0;
}

Output:

Output

The value of the load balancer: 1
The max load factor of the sample after setting it: 300
The elements of the sample {40, 400} {10, 300}

Input Required

This code uses input(). Please provide values below: