unordered_multimap rehash() function in C++ STL

The unordered_multimap::rehash(N) is a built-in function in C++ STL which 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 keys is preserved. Rehashes are automatically performed by the container whenever its load factor is going to surpass its max_load_factor in an operation. 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.

Syntax:

unordered_multimap_name.rehash(N)

Parameters: The function accepts a single mandatory parameter N which specifies the minimum number of buckets for the container hash table.

Return Value: The function does not return anything.

Below programs illustrates the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_multimap::rehash()
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<int, int> sample1, sample2;
  
    // the sample1 size is reserved for
    // the bucket to contain a minimum of
    // one elements
    sample1.rehash(1);
  
    // inserts key and element
    // in sample1
    sample1.insert({ 10, 100 });
    sample1.insert({ 50, 500 });
  
    // inserts key and element
    // in sample1
  
    // the sample1 size is reserved for
    // the bucket to contain a minimum of
    // three elements
    sample2.rehash(3);
  
    sample2.insert({ 20, 200 });
    sample2.insert({ 30, 300 });
    sample2.insert({ 30, 150 });
  
    cout << "The size of Sample1 is: " << sample1.size();
  
    cout << "\nKey and Elements of Sample1 are:";
    for (auto it = sample1.begin(); it != sample1.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }
  
    cout << "\n\nThe size of Sample2 is: " << sample2.size();
  
    cout << "\nKey and Elements of Sample2 are:";
    for (auto it = sample2.begin(); it != sample2.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }
  
    return 0;
}

chevron_right


Output:

The size of Sample1 is: 2
Key and Elements of Sample1 are:{50, 500} {10, 100} 

The size of Sample2 is: 3
Key and Elements of Sample2 are:{30, 150} {30, 300} {20, 200}

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_multimap::rehash()
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<char, char> sample1, sample2;
  
    // the sample1 size is reserved for
    // the bucket to contain a minimum of
    // one elements
    sample1.rehash(1);
  
    // inserts key and element
    // in sample1
    sample1.insert({ 'a', 'A' });
    sample1.insert({ 'g', 'G' });
  
    // inserts key and element
    // in sample1
  
    // the sample1 size is reserved for
    // the bucket to contain a minimum of
    // three elements
    sample2.rehash(3);
  
    sample2.insert({ 'b', 'B' });
    sample2.insert({ 'c', 'C' });
    sample2.insert({ 'd', 'D' });
  
    cout << "The size of Sample1 is: " << sample1.size();
  
    cout << "\nKey and Elements of Sample1 are:";
    for (auto it = sample1.begin(); it != sample1.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }
  
    cout << "\n\nThe size of Sample2 is: " << sample2.size();
  
    cout << "\nKey and Elements of Sample2 are:";
    for (auto it = sample2.begin(); it != sample2.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }
  
    return 0;
}

chevron_right


Output:

The size of Sample1 is: 2
Key and Elements of Sample1 are:{g, G} {a, A} 

The size of Sample2 is: 3
Key and Elements of Sample2 are:{d, D} {c, C} {b, B}

Reference: http://www.cplusplus.com/reference/unordered_map/unordered_multimap/rehash/



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.