unordered_set reserve() in C++

reserve(): This function is available in the File: unordered_set.h. This function is a build-in function in STL of C++. This function reserve the number of elements in unordered_set.

Syntax:

void std::unordered_set<int>::reserve(std::size_t __n)

This function takes one argument of type size_t as its parameter. This function prepare the unordered_set for a specified number of elements.

Parameters: One parameter is required for this method.

__n – Number of elements required.

Same as rehash(ceil(n / max_load_factor())).

File: unordered_set.h

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    unordered_set<intst = {1234};
    int n = 56;

    cout << "Before reserve bucket count is " << 
st.bucket_count() << "\n";
    st.reserve(n);

    cout << "After reserve bucket count is " << 
st.bucket_count() << "\n";
    return 0;
}


Output:

Before reserve bucket count is 5 After reserve bucket count is 59


No comments:

Post a Comment