- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
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