Skip to content
Related Articles

Related Articles

Improve Article

unordered_set load_factor() function in C++ STL

  • Difficulty Level : Basic
  • Last Updated : 28 Sep, 2018

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:




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




// 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;
}
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
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :