public member function
<unordered_map>
get (1) | float max_load_factor() const noexcept;
|
---|
set (2) | void max_load_factor ( float z ); |
---|
Get or set maximum load factor
The first version (1) returns the current maximum load factor for the unordered_multimap container.
The second version (2) sets z as the new maximum load factor for 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).
By default, unordered_multimap containers have a max_load_factor of 1.0.
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 uses the value of max_load_factor as the threshold that forces an increase in the number of buckets (and thus causing a rehash).
Parameters
- z
- The new maximum load factor
Return Value
The current load factor (only for the first version).
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
// unordered_multimap::max_load_factor
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_multimap<std::string,std::string> myumm = {
{"apple","NY"},
{"apple","WA"},
{"peach","GA"},
{"cherry","UT"}
};
std::cout << "current max_load_factor: " << myumm.max_load_factor() << std::endl;
std::cout << "current size: " << myumm.size() << std::endl;
std::cout << "current bucket_count: " << myumm.bucket_count() << std::endl;
std::cout << "current load_factor: " << myumm.load_factor() << std::endl;
float z = myumm.max_load_factor();
myumm.max_load_factor ( z / 2.0 );
std::cout << "[max_load_factor halved]" << std::endl;
std::cout << "new max_load_factor: " << myumm.max_load_factor() << std::endl;
std::cout << "new size: " << myumm.size() << std::endl;
std::cout << "new bucket_count: " << myumm.bucket_count() << std::endl;
std::cout << "new load_factor: " << myumm.load_factor() << std::endl;
return 0;
}
|
Possible output:
current max_load_factor: 1
current size: 4
current bucket_count: 5
current load_factor: 0.8
[max_load_factor halved]
new max_load_factor: 0.5
new size: 4
new bucket_count: 11
new load_factor: 0.363636
|
Complexity
Constant.
May trigger a rehash (not included).
Iterator validity
No changes, unless a change in this value forces a rehash. In this case, all iterators in the container are invalidated.
A rehash is forced if the new container max_load_factor is set below the current load_factor.
References to elements in the unordered_multimap container remain valid in all cases, even after a rehash.