public member function
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.
- The number of elements requested as minimum capacity.
Member type size_type is an unsigned integral type.
int main ()
std::cout << "myset contains:";
for (const std::string& x: myset) std::cout << " " << x;
std::cout << std::endl;
myset contains: highway house office gym parking
By calling reserve with the size we expected for the unordered_set container we avoided the multiple rehashes that the increases in container size could have produced and optimized the size of the hash table.
If a rehash happens, all iterators are invalidated, but references and pointers to individual elements remain valid.
If no actual rehash happens, no changes.