The C++ std::unordered_set::rehash() function is used to set the number of buckets in the unordered_set container to n or more. A rehash is the reconstruction of the hash table; all its elements are rearranged into the new set of buckets according to their new hash value.
Following is the syntax of std::unordered_set::rehash() function.
void rehash ( size_type n );Parameters
This function does not returns anything.
Example 1Let's look at the following example, where we are going to demonstrate the usage of unordered_set::rehash() function.
#include <iostream> #include <string> #include <unordered_set> int main () { std::unordered_set<std::string> myset; myset.rehash(12); myset.insert("android"); myset.insert("java"); myset.insert("html"); myset.insert("css"); myset.insert("javascript"); std::cout << "current bucket_count: " << myset.bucket_count() << std::endl; return 0; }Output
Let us compile and run the above program, this will produce the following result −
current bucket_count: 13Example 2
Consider the following example, where we are going to consider the empty unordered_set which is empty and displaying its initial bucket count and current bucket count after the use of the unordered_set::rehash() function.
#include <iostream> #include <string> #include <unordered_set> using namespace std; int main () { unordered_set<string> uSet; cout<<"Initial bucket count: "<<uSet.bucket_count()<<endl; uSet.rehash(10); cout << "current bucket_count: " << uSet.bucket_count() << endl; return 0; }Output
If we run the above code it will generate the following output −
Initial bucket count: 1 current bucket_count: 11Example 3
Let's look at the another example of the usage of the unordered_set:: rehash() function and displaying the count before and after the use of function.
#include <iostream> #include <string> #include <unordered_set> using namespace std; int main () { unordered_set<int> uSet = {1, 2, 3, 4, 5}; cout<<"Initial bucket count: "<<uSet.bucket_count()<<endl; uSet.rehash(15); cout << "current bucket_count: " << uSet.bucket_count() << endl; return 0; }Output
Following is the output of the above code −
Initial bucket count: 13 current bucket_count: 17Example 4
Following is the example, where we are going to use the unordered_set::rehash() function to set the number of buckets and also display the number of buckets.
#include <iostream> #include <unordered_set> using namespace std; int main() { unordered_set<int> uSet; uSet = { 2, 4, 6, 8 }; cout << "Size of unorder_set container : " << uSet.size() << endl; cout << "Initial bucket count : " << uSet.bucket_count() << endl; uSet.rehash(12); cout << "Size of unorder_set container : " << uSet.size() << endl; cout << "current bucket count is : " << uSet.bucket_count() << endl; for(unsigned int i = 0; i < uSet.bucket_count(); i++){ cout<<"The bucket #"<<i <<endl; } return 0; }Output
Output of the above code is as follows −
Size of unorder_set container : 4 Initial bucket count : 5 Size of unorder_set container : 4 current bucket count is : 13 The bucket #0 The bucket #1 The bucket #2 The bucket #3 The bucket #4 The bucket #5 The bucket #6 The bucket #7 The bucket #8 The bucket #9 The bucket #10 The bucket #11 The bucket #12
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