C++ unordered_set - max_bucket_count() Function
The C++ unordered_set::max_bucket_count function returns the maximum number of buckets that the unordered_set can have. It returns the maximum potential number of buckets the unordered_set can have due to known system or library implementation limitations.
As an unordered_set is implemented using hash table where a bucket is a slot in the container's internal hash table to which elements are assigned based on the hash value. The number of buckets directly influences the load_factor of the container's hash table. The container automatically increases the number of buckets to keep the load_factor below its max_load_factor which causes rehash whenever the number of buckets is increased.
Syntax
size_type max_bucket_count() const noexcept;
Parameters
No parameter is required.
Return Value
The maximum number of buckets in the unordered_set.
Time Complexity
Constant i.e, Θ(1).
Example:
In the example below, the unordered_set::max_bucket_count function is used to find out the maximum number of buckets that the unordered_set can have.
#include <iostream> #include <unordered_set> using namespace std; int main (){ unordered_set<int> uSet; cout<<"Max size = "<<uSet.max_size()<<"\n"; cout<<"Max bucket count = "<<uSet.max_bucket_count()<<"\n"; cout<<"Max load factor = "<<uSet.max_load_factor()<<"\n"; return 0; }
A possible output could be:
Max size = 576460752303423487 Max bucket count = 576460752303423487 Max load factor = 1
❮ C++ <unordered_set> Library