Skip to content
Related Articles

Related Articles

unordered_map reserve() in C++ STL
  • Difficulty Level : Hard
  • Last Updated : 31 Dec, 2018


As we know a Bucket is a slot in the container’s internal hash table to which all the element are assigned based on the hash value of their key . Buckets are numbered from 0 to bucket_count. Now as a Bucket hold variable number of the item . This number is based on the term Load Factor .When the Load Factor(load_factor) reaches a certain threshold, the container increases the number of buckets and rehashes the map.But when we call rehash(n) then it directly sets the number of buckets to n and triggers a rebuild of the entire hash table.But when we call reserve(n) then it create enough Buckets to hold at least n items.If then we add > n items to the map, a rehash may be triggered depending on the load factor. By calling reserve with the size we expected for the unordered_map container we avoided the multiple rehashes that the increases in container size could have produced and optimized the size of the hash table. The C++ function std::unordered_map::reserve() sets the number of buckets in the container (bucket_count) to the most appropriate to contain at least n elements.

Syntax:

unordered_map_name.reserve(N)

Parameters: The function accepts a single mandatory parameter N which specifies the number of elements requested as the minimum capacity.

Return Value: The function does not return anything.

Below programs illustrates the above function:



Program 1:




// C++ program to illustrate the
// unordered_map::reserve()
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_map<int, int> sample1, sample2;
  
    // the sample1 size is reserved for
    // the bucket to contain a minimum of
    // one elements
    sample1.reserve(1);
  
    // inserts key and element
    // in sample1
    sample1.insert({ 10, 100 });
    sample1.insert({ 50, 500 });
  
    // inserts key and element
    // in sample1
  
    // the sample1 size is reserved for
    // the bucket to contain a minimum of
    // three elements
    sample2.reserve(3);
  
    sample2.insert({ 20, 200 });
    sample2.insert({ 30, 300 });
    sample2.insert({ 30, 150 });
  
    cout << "The size of Sample1 is: " << sample1.size();
  
    cout << "\nKey and Elements of Sample1 are:";
    for (auto it = sample1.begin(); it != sample1.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }
  
    cout << "\n\nThe size of Sample2 is: " << sample2.size();
  
    cout << "\nKey and Elements of Sample2 are:";
    for (auto it = sample2.begin(); it != sample2.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }
  
    return 0;
}
Output:
The size of Sample1 is: 2
Key and Elements of Sample1 are:{50, 500} {10, 100} 

The size of Sample2 is: 2
Key and Elements of Sample2 are:{30, 300} {20, 200}

Program 2:




// C++ program to illustrate the
// unordered_map::reserve()
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_map<char, char> sample1, sample2;
  
    // the sample1 size is reserved for
    // the bucket to contain a minimum of
    // one elements
    sample1.reserve(1);
  
    // inserts key and element
    // in sample1
    sample1.insert({ 'a', 'A' });
    sample1.insert({ 'g', 'G' });
  
    // inserts key and element
    // in sample1
  
    // the sample1 size is reserved for
    // the bucket to contain a minimum of
    // three elements
    sample2.reserve(3);
  
    sample2.insert({ 'b', 'B' });
    sample2.insert({ 'c', 'C' });
    sample2.insert({ 'd', 'D' });
  
    cout << "The size of Sample1 is: " << sample1.size();
  
    cout << "\nKey and Elements of Sample1 are:";
    for (auto it = sample1.begin(); it != sample1.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }
  
    cout << "\n\nThe size of Sample2 is: " << sample2.size();
  
    cout << "\nKey and Elements of Sample2 are:";
    for (auto it = sample2.begin(); it != sample2.end(); it++) {
        cout << "{" << it->first << ", " << it->second << "} ";
    }
  
    return 0;
}
Output:
The size of Sample1 is: 2
Key and Elements of Sample1 are:{g, G} {a, A} 

The size of Sample2 is: 3
Key and Elements of Sample2 are:{d, D} {c, C} {b, B}
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :