C++ Unordered_map Library - bucket_count() Function



Description

The C++ function std::unordered_map::bucket_count() returns the number of buckets in unordered_map container.

Declaration

Following is the declaration for std::unordered_map::bucket_count() function form std::unordered_map header.

C++11

size_type bucket_count() const noexcept;

Parameters

None

Return value

Returns the total number of bucket present in the unordered_map.

Exceptions

This member function does not throw exception.

Time complexity

Constant i.e. O(1)

Example

The following example shows the usage of std::unordered_map::bucket_count() function.

#include <iostream>
#include <unordered_map>

using namespace std;

int main(void) {
   unordered_map<char, int> um = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5}
            };

   cout << "Number of buckets = " << um.bucket_count() << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

Number of buckets = 11
unordered_map.htm
Advertisements