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:
CPP
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set< int > sample;
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:
CPP
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
unordered_set< char > sample;
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
Time complexity: O(1)
Last Updated :
05 Jun, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...