Open In App

unordered_multiset load_factor() function in C++ STL

Improve
Improve
Like Article
Like
Save
Share
Report

The unordered_multiset::load_factor() is a built-in function in C++ STL which returns returns the current load factor in the unordered_multiset container. The load factor is the ratio between the number of elements in the container (its size) and the number of buckets (bucket_count):

load_factor = size / bucket_count

The load factor influences the probability of collision in the hash table (i.e., the probability of two elements being located in the same bucket). The container automatically increases the number of buckets to keep the load factor below a specific threshold (its max_load_factor), by causing a rehash each time when an expansion is needed.

Syntax:

unordered_multiset_name.load_factor

Parameter: The function does no accepts any parameter.

Return Value: The function returns the current load factor. It can be of integer or double type.

Below programs illustrate the unordered_multiset::load_factor() function:

Program 1:




// C++ program to illustrate the
// unordered_multiset::load_factor() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multiset<char> sample;
  
    // inserts element
    sample.insert('a');
    sample.insert('b');
  
    cout << "The size is: " << sample.size();
    cout << "\nThe bucket_count is: " << sample.bucket_count();
    cout << "\nThe load_factor is: " << sample.load_factor();
  
    sample.insert('b');
    sample.insert('b');
  
    cout << "\n\nThe size is: " << sample.size();
    cout << "\nThe bucket_count is: " << sample.bucket_count();
    cout << "\nThe load_factor is: " << sample.load_factor();
  
    sample.insert('z');
  
    cout << "\n\nThe size is: " << sample.size();
    cout << "\nThe bucket_count is: " << sample.bucket_count();
    cout << "\nThe load_factor is: " << sample.load_factor();
  
    return 0;
}


Output:

The size is: 2
The bucket_count is: 3
The load_factor is: 0.666667

The size is: 4
The bucket_count is: 7
The load_factor is: 0.571429

The size is: 5
The bucket_count is: 7
The load_factor is: 0.714286

Program 2:




// C++ program to illustrate the
// unordered_multiset::load_factor() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multiset<int> sample;
  
    // inserts element
    sample.insert(1);
    sample.insert(1);
  
    cout << "The size is: " << sample.size();
    cout << "\nThe bucket_count is: " << sample.bucket_count();
    cout << "\nThe load_factor is: " << sample.load_factor();
  
    sample.insert(1);
    sample.insert(2);
  
    cout << "\n\nThe size is: " << sample.size();
    cout << "\nThe bucket_count is: " << sample.bucket_count();
    cout << "\nThe load_factor is: " << sample.load_factor();
  
    sample.insert(2);
  
    cout << "\n\nThe size is: " << sample.size();
    cout << "\nThe bucket_count is: " << sample.bucket_count();
    cout << "\nThe load_factor is: " << sample.load_factor();
  
    return 0;
}


Output:

The size is: 2
The bucket_count is: 3
The load_factor is: 0.666667

The size is: 4
The bucket_count is: 7
The load_factor is: 0.571429

The size is: 5
The bucket_count is: 7
The load_factor is: 0.714286


Last Updated : 02 Aug, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads