Skip to content
Related Articles

Related Articles

unordered_multiset reserve() in C++ STL
  • Last Updated : 23 Jul, 2019

The reserve() function of unordered_multiset sets the number of buckets in the container (bucket_count) to the most appropriate to contain at least n elements.

If n is greater than the current bucket_count multiplied by the max_load_factor, the container’s bucket_count is increased and a rehash is forced.

If n is lower than that, the function may have no effect.

Syntax:

void reserve( size_type n );

where size_type is an unsigned integral type.



Parameters: This method accepts a mandatory parameter n which is the number of elements requested as minimum capacity.

Returns: It does not returns any value.

Below are the programs to illustrate reserve() method:

Example 1:




#include <iostream>
#include <unordered_set>
  
using namespace std;
  
int main()
{
    unordered_multiset<int> j;
  
    // container invoked
    // it reverse the values
    j.reserve(5);
  
    // set the values of the container
    j.insert(5);
    j.insert(6);
    j.insert(7);
  
    cout << "The values in unordered_multiset :";
    for (const int& x : j)
        cout << " " << x;
  
    return 0;
}
Output:
The values in unordered_multiset : 7 6 5

Example 2:




#include <iostream>
#include <unordered_set>
  
using namespace std;
  
int main()
{
    unordered_multiset<string> j;
  
    // container invoked
    // it reverse the values
    j.reserve(5);
  
    // set the values of the container
    j.insert("Geeks");
    j.insert("forGeeks");
    j.insert("GeeksforGeeks");
  
    cout << "The values in unordered_multiset :";
    for (const string& x : j)
        cout << " " << x;
  
    return 0;
}
Output:
The values in unordered_multiset : GeeksforGeeks forGeeks Geeks
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 :