unordered_multimap load_factor() function in C++ STL
The unordered_multimap::load_factor() is a built-in function in C++ STL which returns returns the current load factor in the unordered_multimap 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_multimap_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_multimap::load_factor() function:
Program 1:
// C++ program to illustrate the // unordered_multimap::load_factor() function #include <iostream> #include <unordered_map> using namespace std; int main() { // declaration unordered_multimap< int , int > sample; // inserts element sample.insert({ 1, 2 }); sample.insert({ 1, 3 }); sample.insert({ 2, 4 }); sample.insert({ 5, 8 }); sample.insert({ 7, 10 }); 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, 1 }); sample.insert({ 9, 0 }); 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({ 1, 1 }); 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; } |
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_multimap::load_factor() #include <iostream> #include <unordered_map> using namespace std; int main() { // declaration unordered_multimap< char , char > sample; // inserts element sample.insert({ 'a' , 'b' }); sample.insert({ 'a' , 'b' }); sample.insert({ 'a' , 'd' }); sample.insert({ 'b' , 'e' }); sample.insert({ 'b' , '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({ 'b' , 'k' }); sample.insert({ 'b' , 'h' }); 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' , 'j' }); 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; } |
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
Contact Us