C++ Unordered_set::reserve() function
The C++ unordered_set::reserve() function is used to change the capacity of the bucket. It sets the number of buckets in the container (bucket_count) to the most appropriate to contain at least n elements without exceeding maximum load factor and rehashes the container.
A rehash is forced when n is greater than the current bucket_count multiplied by the max_load_factor, and increase in the container bucket count, causes a rehash.
Syntax
Following is the syntax of std::unordered_set::reserve() function.
void reserve ( size_type n );
Parameters
- n − It inidicates the minimum number of buckets.
Return Value
This function does not returns anything.
Example 1
Let's look at the following example, where we are going to demonstrate the usage of unordered_set::reserve() function.
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
int main () {
unordered_set<string> uSet;
uSet.reserve(3);
uSet.insert("android");
uSet.insert("java");
uSet.insert("html");
cout << "uSet contains:";
for (const string& x: uSet) cout << " " << x;
cout << endl;
cout<<"After reserve bucket count is: "<< uSet.bucket_count();
return 0;
}
Output
If we run the above code it will generate the following output −
uSet contains: html java android After reserve bucket count is: 3
Example 2
In the following example, we are going to count the number of buckets before and after the use of the unordered_set::reserve() function.
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
int main () {
unordered_set<string> uSet = {"android", "java", "Html", "CSS"};
cout<<"Initial bucket count is: "<< uSet.bucket_count() << endl;
uSet.reserve(3);
cout << "uSet contains:";
for (const string& x: uSet)
cout << " " << x;
cout << endl;
cout<<"After reserve bucket count is: "<< uSet.bucket_count();
return 0;
}
Output
Following is the output of the above code −
Initial bucket count is: 13 uSet contains: android java Html CSS After reserve bucket count is: 5
Example 3
Consider the following example, where we are going to display the buckets and their elements before and after the use of the unordered_set::reserve() function.
#include <iostream>
#include <unordered_set>
using namespace std;
int main () {
unordered_set<string> uSet={"Hyderabad", "Delhi", "Bangalore"};
cout<<"unordered_set contains "<<uSet.bucket_count()<<" buckets:";
for(unsigned int i = 0; i < uSet.bucket_count(); i++) {
cout<<"\nThe bucket "<<i<<" contains: ";
for(auto it = uSet.begin(i); it != uSet.end(i); ++it) {
cout<<*it<<" ";
}
}
cout<<"\n***Capacity is changed using reserve function***\n";
uSet.reserve(5);
cout<<"unordered_set contains "<<uSet.bucket_count()<<" buckets:";
for(unsigned int i = 0; i < uSet.bucket_count(); i++) {
cout<<"\nThe bucket "<<i<<" contains: ";
for(auto it = uSet.begin(i); it != uSet.end(i); ++it) {
cout<<*it<<" ";
}
}
return 0;
}
Output
Output of the above code is as follows −
unordered_set contains 13 buckets: The bucket 0 contains: Bangalore The bucket 1 contains: The bucket 2 contains: Hyderabad The bucket 3 contains: Delhi The bucket 4 contains: The bucket 5 contains: The bucket 6 contains: The bucket 7 contains: The bucket 8 contains: The bucket 9 contains: The bucket 10 contains: The bucket 11 contains: The bucket 12 contains: ***Capacity is changed using reserve function*** unordered_set contains 5 buckets: The bucket 0 contains: The bucket 1 contains: Bangalore The bucket 2 contains: The bucket 3 contains: Delhi The bucket 4 contains: Hyderabad