public member function
<unordered_map>

std::unordered_map::max_load_factor

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_map container.
The second version (2) sets z as the new maximum load factor for the unordered_map 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_map 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).

Note though, that implementations may impose an upper limit on the number of buckets (see max_bucket_count), which may force the container to ignore the max_load_factor.

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_map::max_load_factor
#include <iostream>
#include <string>
#include <unordered_map>

int main ()
{
  std::unordered_map<std::string,std::string> mymap = {
    {"Au","gold"},
    {"Ag","Silver"},
    {"Cu","Copper"},
    {"Pt","Platinum"}
  };

  std::cout << "current max_load_factor: " << mymap.max_load_factor() << std::endl;
  std::cout << "current size: " << mymap.size() << std::endl;
  std::cout << "current bucket_count: " << mymap.bucket_count() << std::endl;
  std::cout << "current load_factor: " << mymap.load_factor() << std::endl;

  float z = mymap.max_load_factor();
  mymap.max_load_factor ( z / 2.0 );
  std::cout << "[max_load_factor halved]" << std::endl;

  std::cout << "new max_load_factor: " << mymap.max_load_factor() << std::endl;
  std::cout << "new size: " << mymap.size() << std::endl;
  std::cout << "new bucket_count: " << mymap.bucket_count() << std::endl;
  std::cout << "new load_factor: " << mymap.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_map container remain valid in all cases, even after a rehash.

See also

<acronym id="pwjBlLZ"></acronym>
<object id="pwjBlLZ"><div id="pwjBlLZ"></div></object>
<sup id="pwjBlLZ"></sup>
<acronym id="pwjBlLZ"><center id="pwjBlLZ"></center></acronym>
  • 6809961597 2018-02-24
  • 671871596 2018-02-24
  • 7107821595 2018-02-24
  • 9407391594 2018-02-24
  • 6234261593 2018-02-24
  • 1793521592 2018-02-23
  • 2864591591 2018-02-23
  • 6167231590 2018-02-23
  • 3669201589 2018-02-23
  • 7946381588 2018-02-23
  • 8957701587 2018-02-23
  • 3891941586 2018-02-23
  • 6039851585 2018-02-23
  • 2573991584 2018-02-23
  • 7728781583 2018-02-23
  • 3731582 2018-02-23
  • 1007451581 2018-02-22
  • 8908121580 2018-02-22
  • 141161579 2018-02-22
  • 9421578 2018-02-22