C++ Standard Library C++ STL Library

C++ unordered_map - max_bucket_count() Function



The C++ unordered_map::max_bucket_count function returns the maximum number of buckets that the unordered_map can have. It returns the maximum potential number of buckets the unordered_map can have due to known system or library implementation limitations.

As an unordered_map 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 of their key. 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_map.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the unordered_map::max_bucket_count function is used to find out the maximum number of buckets that the unordered_map can have.

#include <iostream>
#include <unordered_map>
using namespace std;
 
int main (){
  unordered_map<string, string> uMap;

  cout<<"Max size = "<<uMap.max_size()<<"\n";
  cout<<"Max bucket count = "<<uMap.max_bucket_count()<<"\n";
  cout<<"Max load factor = "<<uMap.max_load_factor()<<"\n";

  return 0;
}

A possible output could be:

Max size = 115292150460684697
Max bucket count = 115292150460684697
Max load factor = 1

❮ C++ <unordered_map> Library