C++ unordered_multimap::reserve() Function
The C++ std::unordered_multimap::reserve() function is used to sets the number of buckets in the container to the most appropriate to contain at least n elements without exceeding maximum load factor and rehashes the container.
If n is greater than the current bucket_count() * max_load_factor() then the containers bucket count is increased and a rehash is forced similarly if n is lower than that, the function may have no effect.
Syntax
Following is the syntax for std::unordered_multimap::reserve() function.
void reserve(size_type n);
Parameters
- n − It indicates the capacity of the container.
Return value
This function does not return anything.
Example 1
In the following example, let's see the usage of unordered_multimap::reserve() function.
#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
unordered_multimap<char, int> umm;
cout << "Initial bucket count = " << umm.bucket_count() << endl;
umm.reserve(5);
cout << "Bucket count after reserve = "
<< umm.bucket_count() << endl;
return 0;
}
Output
On executing the above code, we get the following output, which shows the initial bucket count and reserve count −
Initial bucket count = 1 Bucket count after reserve = 5
Example 2
Consider the following example, where we are going to use the reserve() function to make the bucket count to store at least 5 element.
#include <iostream>
#include <unordered_map>
using namespace std;
int main () {
unordered_multimap<string, string> umMap;
cout << "Bucket Count: " << umMap.bucket_count() << endl;
umMap.reserve(5);
cout << "Bucket Count after reserve(): " << umMap.bucket_count() << endl;
umMap.insert({ {"Fname", "Tutorix"}, {"Fname", "tutorials"},{"Lname", "Point"}, {"Country","India"}, {"Locaton","Hyderabad"}});
for (auto& it: umMap)
cout << it.first << "->" << it.second << endl;
return 0;
}
Output
On executing the above code, we get the bucket count in both the initial and reserve containers, as well as the element of the container.
Bucket Count: 1 Bucket Count after reserve(): 5 Country->India Lname->Point Locaton->Hyderabad Fname->tutorials Fname->Tutorix
Example 3
Let's look at the following example, where we are going to display the bucket with its elements before and after the use of the reserve() function.
#include <iostream>
#include <unordered_map>
using namespace std;
int main () {
unordered_multimap<string, string> umMap={{"Hyderabad", "India"}, {"Delhi", "India"}, {"Bangalore", "India"}, {"Hyderabad", "Telngana"}};
cout<<"Unordered_multimap contains "<<umMap.bucket_count()<<" buckets:";
for(unsigned int i = 0; i < umMap.bucket_count(); i++) {
cout<<"\nThe bucket "<<i<<" contains: ";
for(auto it = umMap.begin(i); it != umMap.end(i); ++it) {
cout<<it->first<<":"<<it->second<<" ";
}
}
cout<<"\nCapacity is changed using reserve function.\n";
umMap.reserve(5);
cout<<"Unordered_multimap contains "<<umMap.bucket_count()<<" buckets:";
for(unsigned int i = 0; i < umMap.bucket_count(); i++) {
cout<<"\nThe bucket "<<i<<" contains: ";
for(auto it = umMap.begin(i); it != umMap.end(i); ++it) {
cout<<it->first<<":"<<it->second<<" ";
}
}
return 0;
}
Output
On executing the above code, we get the number of buckets in both initial and reserve conditions and their elements −
Unordered_multimap contains 5 buckets: The bucket 0 contains: The bucket 1 contains: Bangalore:India The bucket 2 contains: The bucket 3 contains: Delhi:India The bucket 4 contains: Hyderabad:Telngana Hyderabad:India Capacity is changed using reserve function. Unordered_multimap contains 5 buckets: The bucket 0 contains: The bucket 1 contains: Bangalore:India The bucket 2 contains: The bucket 3 contains: Delhi:India The bucket 4 contains: Hyderabad:Telngana Hyderabad:India