C++ unordered_set load_factor() Function
Example
Check that the load factor is between 0 and 1:
unordered_set<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
float lf = cars.load_factor();
if (lf > 0 && lf <= 1) {
cout << "The load factor is between 0 and 1";
}
Definition and Usage
The load_factor() function returns the average number of elements in each bucket (a group that holds elements). It is the number of elements divided by the number of buckets.
When the load factor would go above 1.0 (the default maximum), the unordered set adds more buckets automatically. So with the default settings, the load factor stays between 0 and 1.
The load_factor function is defined in the <unordered_set> header file.
Note: The exact load factor depends on the compiler, so the example does not print it. Use bucket_count() to get the number of buckets.
Syntax
unordered_set.load_factor()
Parameter Values
None.
Technical Details
| Returns: | A float value: the number of elements divided by the number of buckets. |
|---|
Related Pages
Read more about unordered sets in our Unordered Set Tutorial.