public member function
<unordered_set>
std::unordered_set::reservevoid 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// unordered_set::reserve
#include <iostream>
#include <string>
#include <unordered_set>
int main ()
{
std::unordered_set<std::string> myset;
myset.reserve(5);
myset.insert("office");
myset.insert("house");
myset.insert("gym");
myset.insert("parking");
myset.insert("highway");
std::cout << "myset contains:";
for (const std::string& x: myset) std::cout << " " << x;
std::cout << std::endl;
return 0;
}
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4