Unordered Multimap Rehash Function In C++ - C++ Programming Tutorial
C++ Course / STL Set & Map / Unordered Multimap Rehash Function In C++

Unordered Multimap Rehash Function In C++

BLUF: Mastering Unordered Multimap Rehash Function In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Unordered Multimap Rehash Function In C++

C++ is renowned for its efficiency. Learn how Unordered Multimap Rehash Function In C++ enables low-level control and high-performance computing in the tutorial below.

The rehash(N) function in C++ expands the quantity of elements within the container to n or greater. When n surpasses the current element count in the container, a rehash becomes essential. The updated element count could equal or exceed n. In circumstances where n is lower than the existing element count in the container, the function's outcome may not impact the bucket count, thereby eliminating the need for a rehash operation. This function, rehash, has an input parameter of n, specifying the minimum number of buckets for the hash table structure.

A rehash involves restructuring the hash table by redistributing all elements into a fresh set of buckets according to their hash values. This process can alter the sequence in which items are accessed within the container, while ensuring that items with the same keys maintain their relative positions. The container automatically performs rehashes when its load factor surpasses the maxloadfactor during an operation. To avoid numerous rehashes triggered by container expansion, we proactively execute a rehash to allocate a specific minimum quantity of buckets in the hash table.

Syntax:

It has the following syntax:

Example

unordered_multimap_name.rehash(N)

The function accepts a crucial parameter, N, which specifies the minimum quantity of buckets for the hash table of the container.

The function does not return any value.

Algorithm

Example

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 consider a scenario to demonstrate the application of the rehash(N) function in C++'s unordered_multimap.

Example

// 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:

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 consider another instance to demonstrate the application of the rehash(N) function in C++ for unordered_multimap.

Example

// 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:

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}

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience