public member function
<unordered_set>

std::unordered_set::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_set container.
The second version (2) sets z as the new maximum load factor for the unordered_set 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_set 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
// unordered_set::max_load_factor
#include <iostream>
#include <string>
#include <unordered_set>

int main ()
{
  std::unordered_set<std::string> myset =
  {"New York", "Paris", "London", "Hong Kong", "Bangalore", "Tel Aviv"};

  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;
}


Possible output:
current max_load_factor: 1
current size: 6
current bucket_count: 7
current load_factor: 0.857143
[max_load_factor halved]
new max_load_factor: 0.5
new size: 6
new bucket_count: 13
new load_factor: 0.461538

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_set container remain valid in all cases, even after a rehash.

See also

  • 9316991614 2018-02-25
  • 191841613 2018-02-25
  • 16411612 2018-02-25
  • 6081981611 2018-02-25
  • 1784561610 2018-02-25
  • 4651609 2018-02-25
  • 9348111608 2018-02-25
  • 2513771607 2018-02-25
  • 6478461606 2018-02-24
  • 4985791605 2018-02-24
  • 5637141604 2018-02-24
  • 282181603 2018-02-24
  • 6217941602 2018-02-24
  • 5076141601 2018-02-24
  • 714281600 2018-02-24
  • 6607141599 2018-02-24
  • 949041598 2018-02-24
  • 6809961597 2018-02-24
  • 671871596 2018-02-24
  • 7107821595 2018-02-24