Skip to content
Related Articles

Related Articles

unordered_multiset rehash() function in C++ STL
  • Last Updated : 09 Oct, 2018

The unordered_multiset::rehash() is a built-in function in C++ STL which sets the number of buckets in the container to N or more. If N is greater than the current number of buckets in the container (bucket_count), a rehash is forced.

The new bucket count can either be equal or greater than N. If n is lower than the current number of buckets in the container (bucket_count), the function may have no effect on the bucket count and may not force a rehash.

A rehash is the reconstruction of the hash table: All the elements in the container are rearranged according to their hash value into the new set of buckets. This may alter the order of iteration of elements within the container, although the relative order of the elements with equivalent keys is preserved. Rehashes are automatically performed by the container whenever its load factor is going to surpass its max_load_factor in an operation. By calling rehash to reserve a certain minimum amount of buckets in the hash table, we avoid the multiple rehashes that the expansion of the container may cause.

Syntax:

unordered_multset_name.rehash(N);

Parameters: The function accepts only one parameter as listed below:



  • count: It is the new number of buckets.

Return Value: none(void type function)

Below programs illustrate the above method:

Program 1:




// C++ program to illustrate
// unordered_multiset::rehash()
#include <iostream>
#include <unordered_set>
using namespace std;
  
// function to diplay values in multiset
void display(unordered_multiset<int> s)
{
    for (auto it = s.begin(); it != s.end(); it++)
        cout << *it << endl;
}
  
int main()
{
    // declaration
    unordered_multiset<int> s1;
  
    // inserting initial values
    s1.insert(1);
    s1.insert(2);
    s1.insert(3);
  
    // displaying Initial values
    cout << "Initial values are:\n";
    display(s1);
    cout << endl;
  
    // diplaying initial parameters
    cout << "initial parameters are: \n";
    cout << "bucketcount() = " << s1.bucket_count() << endl;
    cout << "load factor = " << s1.load_factor() << endl;
    cout << "Max_load_factor = " << s1.max_load_factor() << endl;
    cout << endl;
  
    // performing rehash
    s1.rehash(200);
  
    // displaying final parameters
    cout << "final parameters are: \n";
    cout << "bucketcount() = " << s1.bucket_count() << endl;
    cout << "load factor = " << s1.load_factor() << endl;
    cout << "Max_load_factor = " << s1.max_load_factor() << endl;
    cout << endl;
  
    return 0;
}
Output:
Initial values are:
3
1
2

initial parameters are: 
bucketcount() = 7
load factor = 0.428571
Max_load_factor = 1

final parameters are: 
bucketcount() = 211
load factor = 0.014218
Max_load_factor = 1

Program 2:




// C++ program to illustrate
// unordered_multiset::rehash()
#include <iostream>
#include <unordered_set>
using namespace std;
  
// function to diplay values in multiset
void display(unordered_multiset<char> s)
{
    for (auto it = s.begin(); it != s.end(); it++)
        cout << *it << endl;
}
int main()
{
    // declaration
    unordered_multiset<char> s1;
  
    // inserting initial values
    s1.insert('a');
    s1.insert('b');
    s1.insert('c');
  
    // displaying Initial values
    cout << "Initial values are:\n";
    display(s1);
    cout << endl;
  
    // diplaying initial parameters
    cout << "initial parameters are: \n";
    cout << "bucketcount() = " << s1.bucket_count() << endl;
    cout << "load factor = " << s1.load_factor() << endl;
    cout << "Max_load_factor = " << s1.max_load_factor() << endl;
    cout << endl;
  
    // performing rehash
    s1.rehash(200);
  
    // displaying final parameters
    cout << "final parameters are: \n";
    cout << "bucketcount() = " << s1.bucket_count() << endl;
    cout << "load factor = " << s1.load_factor() << endl;
    cout << "Max_load_factor = " << s1.max_load_factor() << endl;
    cout << endl;
      
    return 0;
}
Output:
Initial values are:
c
a
b

initial parameters are: 
bucketcount() = 7
load factor = 0.428571
Max_load_factor = 1

final parameters are: 
bucketcount() = 211
load factor = 0.014218
Max_load_factor = 1
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 :