public member function
<unordered_set>
std::unordered_multiset::rehashvoid rehash ( size_type n );
Set number of buckets
Sets the number of buckets in the container to n or more.If n is greater than the current number of buckets in the container (bucket_count), a rehash is forced. The new bucket count can either be equal or greater than n.
If n is lower than the current number of buckets in the container (bucket_count), the function may have no effect on the bucket count and may not force a rehash.
A rehash is the reconstruction of the hash table: All the elements in the container are rearranged according to their hash value into the new set of buckets. This may alter the order of iteration of elements within the container, although the relative order of the elements with equivalent values is preserved.
Rehashes are automatically performed by the container whenever its load factor is going to surpass its max_load_factor in an operation.
Notice that this function expects the number of buckets as argument. A similar function exists, unordered_multiset::reserve, that expects the number of elements in the container as argument.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// unordered_multiset::rehash
#include <iostream>
#include <string>
#include <unordered_set>
int main ()
{
std::unordered_multiset<std::string> myums;
myums.rehash(12);
myums.insert("red");
myums.insert("red");
myums.insert("blue");
myums.insert("green");
myums.insert("yellow");
std::cout << "current bucket_count: " << myums.bucket_count() << std::endl;
return 0;
}
By calling rehash to reserve a certain minimum amount of buckets in the hash table, we avoid the multiple rehashes that the expansion of the container may cause.
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