What is the Unordered_map::max_bucket_count() function in C++?

Overview

unordered_map::max_bucket_count() is available in the <unordered_map> header file in C++.

The unordered_map::max_bucket_count() function is used to obtain the maximum number of buckets an unordered map can have.

Syntax

The syntax of the unordered_map::max_bucket_count() function is given below:

unsigned int max_bucket_count();

Parameter

The unordered_map::max_bucket_count() function does not accept any parameter.

Return

The unordered_map::max_bucket_count() returns the maximum number of buckets an unordered map can have.

Code

Let’s have a look at the code.

#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<string,int> umap = {
{"C++", 10},
{"Java", 20},
{"Python", 30},
{"JavaScript", 40}
};
cout << "Number of elements in the unordered map: " << umap.size();
cout << endl;
cout << "Maximum bucket count is: " << umap.max_bucket_count();
return 0;
}

Explanation

  • Line 1 and 2: We imported the required header files.

  • Line 5: We made a main() function.

  • Line 7 to 12: We initialized an unordered map with integer type keys and string type values. We initialized the unordered map with four key-value pairs.

  • Line 13: We displayed the number of elements in the unordered map using the unordered_map::size() function.

  • Line 15: We displayed the maximum number of buckets an unordered map can have using the unordered_map::max_bucket_count() function.

So, this is how to use the unordered_map::max_bucket_count() function to find the bucket capacity in C++.

Free Resources