unordered_multiset load_factor() function in C++ STL

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.