The unordered_multimap rehash(N) function in C++ increases the number of elements in the container to n or more. If n is more than the number of elements in the container, a rehash is required. The new element count might be either equal to or more than n. If n is less than the present number of elements in the container, the function's result may not affect the bucket count and will not require a rehash function. Rehash returns nothing and takes n as an argument, which determines the lowest possible number of buckets for the frame of the hash table.
A rehash is the rebuilding of the hash table. All the components in the container are reorganized into a new set of buckets based on their hash value. It may change the order in which elements within the container are iterated, but the relative order of elements with equivalent keys is retained. Rehashes are carried out automatically by the container's memory whenever its load factor in an operation exceeds its maxloadfactor . We prevent several rehashes caused by container expansion by executing rehash to reserve a particular minimum number of buckets in the hash table.
Syntax:
It has the following syntax:
unordered_multimap_name.rehash(N)
Parameters: The function receives a single important argument, N, which determines the minimum number of buckets for the container's hash table.
Return Value: Nothing is returned by the function.
Algorithm
Begin
Declaring a map container that is empty m.
Forcing the rehash () method to limit the number of buckets in a container to a minimum.
Insert at least as many key-value pairs as there are buckets in the container.
Print the map container's components.
End.
Example 1:
Let's take an example to illustrate the use of unordered_multimap rehash(N) function in C++.
// Program to implement the
// unordered_multimap::rehash()
#include <bits/stdc++.h>
using namespace std;
int main()
{
// declaration of the variables
unordered_multimap<int, int> s1, s2;
//he s1 size has been set for buckets with a minimum of one element.
s1.rehash(1);
// insertion of elements in s1
s1.insert({ 20, 300 });
s1.insert({ 40, 800 });
//inserts key and element into sample1
//The s1 size has been set for buckets with a minimum of three items.
s2.rehash(3);
s2.insert({ 40, 400 });
s2.insert({ 20, 700 });
s2.insert({ 200, 350 });
cout << "The size of the sample s1 is: " << s1.size();
cout << "\nkey value of sample s1 is:";
for (auto ite = s1.begin(); ite != s1.end(); ite++) {
cout << "{" << ite->first << ", " << ite->second << "} ";
}
cout << "\n\nThe size of the sample s2 is:" << s2.size();
cout << "\nkey value of sample s1 is:";
for (auto ite = s2.begin(); ite != s2.end(); ite++) {
cout << "{" << ite->first << ", " << ite->second << "} ";
}
return 0;
}
Output:
The size of the sample s1 is: 2
key value of sample s1 is:{40, 800} {20, 300}
The size of the sample s2 is:3
key value of sample s1 is:{200, 350} {20, 700} {40, 400}
Example 2:
Let's take another example to illustrate the use of unordered_multimap rehash(N) function in C++.
// Program to implement
// unordered_multimap::rehash()
#include <bits/stdc++.h>
using namespace std;
int main()
{
// declaration of variables of map
unordered_multimap<char, char> s1, s2;
// The sample size is set to ensure that the bucket has at least one element.
s1.rehash(1);
//Inserting the key and elements
// in s1
s1.insert({ 'b', 'B' });
s1.insert({ 'f', 'F' });
//inserts key and element into s1
//The examples of s1 size are reserved for buckets with a minimum of three items.
s2.rehash(3);
s2.insert({ 'd', 'D' });
s2.insert({ 'l', 'L' });
s2.insert({ 'm', 'M' });
cout << "The sample s2 size is: " << s1.size();
cout << "\nThe values of sample s2:";
for (auto ite = s1.begin(); ite != s1.end(); ite++) {
cout << "{" << ite->first << ", " << ite->second << "} ";
}
cout << "\nThe sample s2 size is :" << s2.size();
cout << "\nThe values of sample s2::";
for (auto ite = s2.begin(); ite != s2.end(); ite++) {
cout << "{" << ite->first << ", " << ite->second << "} ";
}
return 0;
}
Output:
The sample s2 size is: 2
The values of sample s2:{f, F} {b, B}
The sample s2 size is :3
The values of sample s2::{l, L} {m, M} {d, D}