unordered_set rehash() in C++

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

Syntax:

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

This function takes one argument of type size_t as its parameter. This function may rehash the unordered_set.

Parameters: One parameter is required for this method.

__n – The new number of buckets.

Rehash will occur only if the new number of buckets respects the unordered_set maximum load factor.

File: unordered_set.h

Approach

C++

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

int main()
{
    unordered_set<intst = {12345};

    int n = 56;

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

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


Output:

Before rehash bucket count is 7 After rehash bucket count is 59


No comments:

Post a Comment