
- 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::rehash() function
The C++ std::unordered_set::rehash() function is used to set the number of buckets in the unordered_set container to n or more. A rehash is the reconstruction of the hash table; all its elements are rearranged into the new set of buckets according to their new hash value.
- If n is greater than the current number of buckets in the container, then the rehash is occurred.
- If n is lower than the current number of buckets in the container, the function may have no effect on the bucket count and may not force a rehash.
Syntax
Following is the syntax of std::unordered_set::rehash() function.
void rehash ( size_type n );
Parameters
- n − It indicates 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::rehash() function.
#include <iostream> #include <string> #include <unordered_set> int main () { std::unordered_set<std::string> myset; myset.rehash(12); myset.insert("android"); myset.insert("java"); myset.insert("html"); myset.insert("css"); myset.insert("javascript"); std::cout << "current bucket_count: " << myset.bucket_count() << std::endl; return 0; }
Output
Let us compile and run the above program, this will produce the following result −
current bucket_count: 13
Example 2
Consider the following example, where we are going to consider the empty unordered_set which is empty and displaying its initial bucket count and current bucket count after the use of the unordered_set::rehash() function.
#include <iostream> #include <string> #include <unordered_set> using namespace std; int main () { unordered_set<string> uSet; cout<<"Initial bucket count: "<<uSet.bucket_count()<<endl; uSet.rehash(10); cout << "current bucket_count: " << uSet.bucket_count() << endl; return 0; }
Output
If we run the above code it will generate the following output −
Initial bucket count: 1 current bucket_count: 11
Example 3
Let's look at the another example of the usage of the unordered_set:: rehash() function and displaying the count before and after the use of function.
#include <iostream> #include <string> #include <unordered_set> using namespace std; int main () { unordered_set<int> uSet = {1, 2, 3, 4, 5}; cout<<"Initial bucket count: "<<uSet.bucket_count()<<endl; uSet.rehash(15); cout << "current bucket_count: " << uSet.bucket_count() << endl; return 0; }
Output
Following is the output of the above code −
Initial bucket count: 13 current bucket_count: 17
Example 4
Following is the example, where we are going to use the unordered_set::rehash() function to set the number of buckets and also display the number of buckets.
#include <iostream> #include <unordered_set> using namespace std; int main() { unordered_set<int> uSet; uSet = { 2, 4, 6, 8 }; cout << "Size of unorder_set container : " << uSet.size() << endl; cout << "Initial bucket count : " << uSet.bucket_count() << endl; uSet.rehash(12); cout << "Size of unorder_set container : " << uSet.size() << endl; cout << "current bucket count is : " << uSet.bucket_count() << endl; for(unsigned int i = 0; i < uSet.bucket_count(); i++){ cout<<"The bucket #"<<i <<endl; } return 0; }
Output
Output of the above code is as follows −
Size of unorder_set container : 4 Initial bucket count : 5 Size of unorder_set container : 4 current bucket count is : 13 The bucket #0 The bucket #1 The bucket #2 The bucket #3 The bucket #4 The bucket #5 The bucket #6 The bucket #7 The bucket #8 The bucket #9 The bucket #10 The bucket #11 The bucket #12