![The C++ Standard Library](/cpp_standard_library/images/cpp-mini-lib-logo.jpg)
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Unordered_set::max_load_factor() Function
The C++ std::unordered_set::max_load_factor() function is used to get and set the maximum load factor in the unordered_set container. It automatically increase the number of buckets if the load factor exceeds this threshold, the default value of max_load_factor is 1.
The load factor influences the probability of collision in the hash table. The container uses the value of max_load_factor as the threshold that forces an increase in the number of buckets and thus causing a rehash.
This function has 2 polymorphic variants: the first return the maximum load facctor and the second one sets a new load factor (you can find the syntaxes of all the variants below).
Syntax
Following is the syntax of std::unordered_set::max_load_factor() function.
float max_load_factor() const; or void max_load_factor ( float new_size );
Parameters
- new_size − It indicates the maximum load factor.
Return Value
This function returns the current load factor.
Example 1
Let's look at the following example, where we are going to demonstrate the usage of unordered_set::max_load_factor() function.
#include <iostream> #include <string> #include <unordered_set> int main () { std::unordered_set<std::string> myset = {"sai", "Ram", "krishna", "prasad", "Bangalore", "india"}; std::cout << "current max_load_factor: " << myset.max_load_factor() << std::endl; std::cout << "current size: " << myset.size() << std::endl; std::cout << "current bucket_count: " << myset.bucket_count() << std::endl; std::cout << "current load_factor: " << myset.load_factor() << std::endl; float z = myset.max_load_factor(); myset.max_load_factor ( z / 2.0 ); std::cout << "[max_load_factor halved]" << std::endl; std::cout << "new max_load_factor: " << myset.max_load_factor() << std::endl; std::cout << "new size: " << myset.size() << std::endl; std::cout << "new bucket_count: " << myset.bucket_count() << std::endl; std::cout << "new load_factor: " << myset.load_factor() << std::endl; return 0; }
Output
Let us compile and run the above program, this will produce the following result −
current max_load_factor: 1 current size: 6 current bucket_count: 13 current load_factor: 0.461538 [max_load_factor halved] new max_load_factor: 0.5 new size: 6 new bucket_count: 13 new load_factor: 0.461538
Example 2
In the following example, we are going to use the unordered_set::max_load_factor() function to get the max_load_factor of the current unordered_set.
#include <iostream> #include <string> #include <unordered_set> using namespace std; int main () { unordered_set<string> uSet; unsigned max_load_factor = uSet.max_load_factor(); cout<<"maximum load factor of empty set: "<<max_load_factor; return 0; }
Output
If we run the above code it will generate the following output −
maximum load factor of empty set: 1
Example 3
Consider the following example, where we are going to get the max_load_factor of the unordered_set and then we set the size inside the max_load_factor() function to check whether the max load factor is different or same after setting the size.
#include <iostream> #include <unordered_set> using namespace std; int main(void) { unordered_set<char> us={'A', 'B', 'c', 'D'}; cout << "Default max_load_factor of unordered_set = "<< us.max_load_factor() << endl; //assigning the size. us.max_load_factor(5.0/2.0); cout<<"max_load_factor after setting the size "<<endl; cout<<"current max_load_factor "<< us.max_load_factor()<<endl; return 0; }
Output
Following is the output of the above code −
default max_load_factor of unordered_set = 1 max_load_factor after setting the size current max_load_factor 2.5
Example 4
Following is the example, where we are going to get the max_load_factor and load_factor for the current unordered_set.
#include <iostream> #include <string> #include <unordered_set> using namespace std; int main () { unordered_set<int> uSet = {1, 2, 3, 4, 5}; float load_factor = uSet.load_factor(); int max_load_factor = uSet.max_load_factor(); cout<<"current load factor: "<< load_factor<<endl; cout<<"current max load factor: "<<max_load_factor<<endl; return 0; }
Output
Output of the above code is as follows −
current load factor: 0.384615 current max load factor: 1