unordered_set load_factor() function in C++ STL

The unordered_set::load_factor() is a built-in function in C++ STL which returns the current load factor in the unordered_set 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_set_name.load_factor()

Parameter: The function does not accept any parameter.

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

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

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_set::load_factor() function
#include <iostream>
#include <unordered_set>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_set<int> sample;
  
    // inserts element
    sample.insert(1);
    sample.insert(11);
    sample.insert(111);
    sample.insert(12);
    sample.insert(13);
  
    cout << "The size is: " << sample.size();
    cout << "\nThe bucket_count is: "
         << sample.bucket_count();
  
    cout << "\nThe load_factor is: "
         << sample.load_factor();
  
    sample.insert(2);
    sample.insert(22);
  
    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(33);
  
    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: 5
The bucket_count is: 7
The load_factor is: 0.714286

The size is: 7
The bucket_count is: 17
The load_factor is: 0.411765

The size is: 8
The bucket_count is: 17
The load_factor is: 0.470588

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_set::load_factor() function
#include <iostream>
#include <unordered_set>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_set<char> sample;
  
    // inserts element
    sample.insert('a');
    sample.insert('b');
    sample.insert('c');
    sample.insert('r');
    sample.insert('d');
  
    cout << "The size is: " << sample.size();
    cout << "\nThe bucket_count is: "
         << sample.bucket_count();
  
    cout << "\nThe load_factor is: "
         << sample.load_factor();
  
    sample.insert('f');
    sample.insert('k');
  
    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: 5
The bucket_count is: 7
The load_factor is: 0.714286

The size is: 7
The bucket_count is: 17
The load_factor is: 0.411765

The size is: 8
The bucket_count is: 17
The load_factor is: 0.470588


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.