Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


The C++ unordered_multimap::load_factor function is used to return current load_factor of the unordered_multimap. The load_factor is defined as ratio of number of elements (its size) in the container and number of buckets in the container (its bucket_count).

load_factor = size / bucket_count

As an unordered_multimap is implemented using hash table where a bucket is a slot in the container's internal hash table to which elements are assigned based on the hash value. The number of buckets directly influences the load_factor of the container's hash table. The container automatically increases the number of buckets to keep the load_factor below its max_load_factor which causes rehash whenever the number of buckets is increased.

Syntax

float load_factor() const noexcept;

Parameters

No parameter is required.

Return Value

The current load factor of the unordered_multimap.

Time Complexity

Constant i.e, Θ(1).

Example:

In the below example, the unordered_multimap::load_factor function is used to find out the current load_factor of uMMap.

#include <iostream>
#include <unordered_map>
using namespace std;
 
int main (){
  unordered_multimap<string, string> uMMap;
  uMMap = {{"CAN", "Ottawa"}, {"USA", "Washington"}, 
           {"IND", "Delhi"}, {"CAN", "Toronto"}};

  cout<<"Size = "<<uMMap.size()<<"\n";
  cout<<"Bucket count = "<<uMMap.bucket_count()<<"\n";
  cout<<"load factor = "<<uMMap.load_factor()<<"\n";  
  cout<<"Max load factor = "<<uMMap.max_load_factor()<<"\n";

  return 0;
}

The output of the above code will be:

Size = 4
Bucket count = 11
load factor = 0.363636
Max load factor = 1

❮ C++ <unordered_map> Library