unordered_multimap rehash() function in C++ STL


The unordered_multimap rehash(N) function in C++ STL sets the number of buckets in the container to n or more. A rehash is forced if n is greater than the current number of buckets in the container. The new bucket count can either be equal to or greater than n. The function may have no effect on the bucket count and may not force a rehash if n is lower than the current number of buckets in the container. Rehash () returns nothing and take n as a parameter which specifies the minimum number of buckets for the container hash table.

Algorithm

Begin
   Declaring an empty map container m.
   Force the reahash() function to restrict number of bucket in a
   container to a minimum amount.
   Insert the key value pairs in the container atleast same as the
   minimum number of buckets.
   Print the elements in the map container. 
End.

Example Code

 Live Demo

#include<iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {
   unordered_map<char, int> m;

   m.rehash(1);
   m.insert (pair<char, int>('b', 10));
   m.insert (pair<char, int>('a', 20));

   cout << "The size is: " << m.size();
   cout << "\nKey and values are: ";
   for (auto it = m.begin(); it != m.end(); it++) {
      cout << "{" << it->first << ", " << it->second << "} ";
   }
   return 0;
}

Output

The size is: 2
Key and values are: {a, 20} {b, 10}

Updated on: 30-Jul-2019

38 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements