unordered_map rehash in C++ STL
The std::unordered_map::rehash() is a built in function C++ STL which which sets the number of buckets in container to n or more.
Syntax
void rehash( size_type s );
- If s is greater than the current buckets into the container then rehashed is done. The new bucket count can be greater than or equal to n.
- If s is less than current bucket count then there may or may not be any effect of function. It totally depends upon compiler
Parameters : It takes New number of buckets into the container.
Return type : Its return type is none.
Example-1:
// C++ code to illustrate the method // unordered_map rehash #include <bits/stdc++.h> using namespace std; int main() { unordered_map< char , int > sample; // Map initialization sample = { { 'a' , 2 }, { 'b' , 4 }, { 'c' , 6 } }; cout << " Size of container : " << sample.size() << endl; cout << " Initial bucket count : " << sample.bucket_count() << endl; // changing rehash sample.rehash(30); cout << " Size of container : " << sample.size() << endl; cout << " Now bucket count is : " << sample.bucket_count() << endl; return 0; } |
chevron_right
filter_none
Output:
Size of container : 3 Initial bucket count : 5 Size of container : 3 Now bucket count is : 31
Example-2:
// C++ code to illustrate the method // unordered_map rehash #include <bits/stdc++.h> using namespace std; int main() { unordered_map< int , int > sample; // Map initialization sample = { { 2, 2 }, { 3, 4 }, { 4, 6 }, { 5, 8 } }; cout << " Size of container : " << sample.size() << endl; cout << " Initial bucket count : " << sample.bucket_count() << endl; // changing rehash sample.rehash(20); cout << " Size of container : " << sample.size() << endl; cout << " Now bucket count is : " << sample.bucket_count() << endl; return 0; } |
chevron_right
filter_none
Output:
Size of container : 4 Initial bucket count : 7 Size of container : 4 Now bucket count is : 23
Recommended Posts:
- unordered_set rehash() function in C++ STL
- unordered_multiset rehash() function in C++ STL
- unordered_multimap rehash() function in C++ STL
- Minimum cells to be flipped to get a 2*2 submatrix with equal elements
- Nested Loops in C++ with Examples
- _Find_first() function in C++ bitset with Examples
- _Find_next() function in C++ bitset with Examples
- Left-Right traversal of all the levels of N-ary tree
- Difference between Iterators and Pointers in C/C++ with Examples
- ostream::seekp(pos) method in C++ with Exmaples
- Default Methods in C++ with Examples
- C++ Tutorial
- Hello World Program : First program while learning Programming
- Difference between Argument and Parameter in C/C++ with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.