Skip to content
Related Articles

Related Articles

unordered_set max_load_factor() in C++ STL
  • Difficulty Level : Basic
  • Last Updated : 09 Oct, 2018

unordered_set::max_load_factor() is a function in C++ STL which returns(Or sets) the current maximum load factor of the unordered set container.

The load factor is the ratio between number of elements in the container and number of buckets(bucket_count). By default the maximum load factor of an unordered set container is set to 1.0 .

max_load_factor()

Syntax:

unordered_set_name.max_load_factor()

Return Value This method returns the current maximum load factor.

Below program illustrate the unordered_set::max_load_factor() method:






// C++ program o illustrate the
// unordered_set::max_load_factor() function
  
#include <iostream>
#include <unordered_set>
  
using namespace std;
  
int main()
{
    unordered_set<int> uset = { 1, 5, 4, 7 };
  
    // Get the max_load_factor of uset
    cout << "Maximum load factor of uset: "
         << uset.max_load_factor()
         << endl;
  
    // Now check the current load factor
    cout << "Current load factor of uset: "
         << uset.load_factor();
}
Output:
Maximum load factor of uset: 1
Current load factor of uset: 0.8

max_load_factor(float)

Syntax

unordered_set_name.max_load_factor(float z)

Parameter: This method takes a floating point number as parameter to which the max_load_factor is to be set.

Return Value: This method does not return any value .

Below program illustrate the unordered_set::max_load_factor(float) method:




// C++ program to illustrate the
// unordered_set::max_load_factor() function
  
#include <iostream>
#include <unordered_set>
using namespace std;
  
int main()
{
    unordered_set<int> uset = { 1, 5, 4, 7 };
  
    // Now set the max_load_factor as  0.5
    uset.max_load_factor(0.5);
  
    // Now get the new max_load_factor of uset
    cout << "New Maximum load factor of uset: "
         << uset.max_load_factor()
         << endl;
  
    // Check the new load factor
    cout << "Current load factor of uset1: "
         << uset.load_factor()
         << endl;
}
Output:
New Maximum load factor of uset: 0.5
Current load factor of uset1: 0.363636
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 :