C++ Unordered_map::reserve() Function



The C++ std::unordered_map::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.

If n is greater than the current bucket_count() then the container's bucket count is increased in the same way if n is lower than the current bucket_count(), then it causes no effect.

Syntax

Following is the syntax for std::unordered_map::reserve() function.

void reserve(size_type n);

Parameters

  • n − It indicates the new capacity of the container.

Return value

This function is void type, So it does not returns anything.

Example 1

In the following example, let's see the usage of the reserve() function.

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_map<char, int> um;
   cout << "Initial bucket count = " << um.bucket_count() << endl;
   um.reserve(5);
   cout << "Bucket count after reserve = "<< um.bucket_count() << endl;
   return 0;
}

Output

Output of the above code is as follows −

Initial bucket count = 1
Bucket count after reserve = 5

Example 2

Consider the following example, where we are going to make the bucket count to store at least 5 element.

#include <iostream>
#include <unordered_map>
using namespace std;
int main () {
   unordered_map<string, string> uMap;
   cout << "Bucket Count: " << uMap.bucket_count() << endl;
   uMap.reserve(5);
   cout << "Bucket Count after reserve(): " << uMap.bucket_count() << endl;
  
   uMap["Fname"] = "tutorials";
   uMap["Lname"] = "Point";
   uMap["Country"] = "India";
   uMap["Locaton"] = "Hyderabad";

   for (auto& it: uMap)
      cout << it.first << "->" << it.second << endl;
   return 0;
}

Output

If we run the above code it will generate the following output −

Bucket Count: 1
Bucket Count after reserve(): 5
Country->India
Lname->Point
Locaton->Hyderabad
Fname->tutorials

Example 3

Let's look at the following example, where we are displaying the buckets and their elements before and after the use of the reserve() function.

#include <iostream>
#include <unordered_map>
using namespace std;
int main () {
   unordered_map<string, string> uMap={{"Hyderabad", "India"}, {"Delhi", "India"}, {"Bangalore", "India"}};
   cout<<"Unordered_map contains "<<uMap.bucket_count()<<" buckets:";
   for(unsigned int i = 0; i < uMap.bucket_count(); i++) {
      cout<<"\nThe bucket "<<i<<" contains: ";   
      for(auto it = uMap.begin(i); it != uMap.end(i); ++it) {
         cout<<it->first<<":"<<it->second<<" ";
      } 
   }  

   cout<<"\nCapacity is changed using reserve function.\n";
   uMap.reserve(5);
   
   cout<<"Unordered_map contains "<<uMap.bucket_count()<<" buckets:";
   for(unsigned int i = 0; i < uMap.bucket_count(); i++) {
      cout<<"\nThe bucket "<<i<<" contains: ";   
      for(auto it = uMap.begin(i); it != uMap.end(i); ++it) {
         cout<<it->first<<":"<<it->second<<" ";
      } 
   }
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

Unordered_map contains 13 buckets:
The bucket 0 contains: 
The bucket 1 contains: Bangalore:India 
The bucket 2 contains: Hyderabad:India 
The bucket 3 contains: Delhi:India 
The bucket 4 contains: 
The bucket 5 contains: 
The bucket 6 contains: 
The bucket 7 contains: 
The bucket 8 contains: 
The bucket 9 contains: 
The bucket 10 contains: 
The bucket 11 contains: 
The bucket 12 contains: 
Capacity is changed using reserve function.
Unordered_map contains 5 buckets:
The bucket 0 contains: Banglore:India 
The bucket 1 contains: 
The bucket 2 contains: 
The bucket 3 contains: Delhi:India 
The bucket 4 contains: Hyderabad:India 
Advertisements