public member function
<unordered_set>

std::unordered_multiset::reserve

void reserve ( size_type n );
Request a capacity change
Sets the number of buckets in the container (bucket_count) to the most appropriate to contain at least n elements.

If n is greater than the current bucket_count multiplied by the max_load_factor, the container's bucket_count is increased and a rehash is forced.

If n is lower than that, the function may have no effect.

Parameters

n
The number of elements requested as minimum capacity.
Member type size_type is an unsigned integral type.

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// unordered_multiset::reserve
#include <iostream>
#include <string>
#include <unordered_set>

int main ()
{
  std::unordered_multiset<std::string> myums;

  myums.reserve(5);

  myums.insert("klingon");
  myums.insert("human");
  myums.insert("vulcan");
  myums.insert("klingon");
  myums.insert("klingon");

  std::cout << "myums contains:";
  for (const std::string& x: myums) std::cout << " " << x;
  std::cout << std::endl;

  return 0;
}


Possible output:
myums contains: human klingon klingon klingon vulcan

By calling reserve with the size we expected for the unordered_multiset container we avoided the multiple rehashes that the increases in container size could have produced and optimized the size of the hash table.

Complexity

In case of rehash,
Average case: linear in container size.
Worst case: quadratic in container size.

Iterator validity

If a rehash happens, all iterators are invalidated, but references and pointers to individual elements remain valid.
If no actual rehash happens, no changes.

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