C++ unordered_multimap::load_factor() Function



The C++ std::unordered_multimap::load_factor() function is used to return the load factor of the unordered_multimap or container. If the unordered multimap contains the 0 elements, then the load factor of that multimap is 0. The load factor is the ratio between the number of elements in the unordered multimap and the number of buckets.

The load factor is calculated as follows −

load_factor = um.size() / um.bucket_count()

Syntax

Following is the syntax of std::unordered_multimap::load_factor() function.

float load_factor() const noexcept;

Parameters

This function does not accepts any parameter.

Return value

This function returns the calculated load factor of the unordered_multimap or container.

Example 1

In the following example, we are calculating the load factor of the unordered_multimap by using load_factor() function.

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_multimap<char, int> umm={{'A', 2}, {'B', 4}, {'B', 5}, {'C', 6}};
   cout << "load_factor of container = " << umm.load_factor() << endl;
   return 0;
}

Output

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

load_factor of container = 0.8

Example 2

Consider the following example, where we are going to consider the multimap that stores the integers type and string type, then we applied the load_factor() function to calculate the load factor.

#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
   unordered_multimap<int, string> umm;
   umm.insert({{1, "tutorialspaoint"}, {1, "tutorix"}, {2, "Hyderabad"}, {3, "Noida"}});
   cout << "load_factor of unordered_multimap = " << umm.load_factor() << endl;
   return 0;
}

Output

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

load_factor of unordered_multimap = 0.307692

Example 3

Let's look at the following example, where we are going to calculate the load factor of the current unordered_multimap using two different methods.

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_multimap<int, int> umm;
   cout<<"Size of multimap "<<umm.size()<<endl;
   cout<<"Size of bucket "<<umm.bucket_count()<<endl;
   unsigned l_factor = umm.size()/umm.bucket_count();
   cout << "load_factor of unordered_multimap = " << l_factor << endl;
   cout << "load_factor of unordered_multimap = " << umm.load_factor() << endl;
   return 0;
}

Output

Following is the output of the above code −

Size of multimap 0
Size of bucket 1
load_factor of unordered_multimap = 0
load_factor of unordered_multimap = 0

Example 4

Following is the example, where we are using the multimap that stores int-type key-value pairs along with the duplicate values then we are going to display the size, bucket_count, and load_factor of the container.

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_multimap<int, int> umm{{1, 10}, {1, 20}, {2, 30}, {2, 40}};
   int size = umm.size();
   int buc_count = umm.bucket_count();
   
   cout<<"Size of multimap "<<size<<endl;
   cout<<"Size of bucket "<<buc_count<<endl;
   
   unsigned l_factor = (size/buc_count);
  
   cout << "load_factor of unordered_multimap = " << umm.load_factor() << endl;
   return 0;
}

Output

Output of the above code is as follows −

Size of multimap 4
Size of bucket 5
load_factor of unordered_multimap = 0.8
Advertisements