C++ std::unordered_map max_bucket_count() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::unordered_map

Definition

In C++, std::unordered_map::max_bucket_count() returns the largest number of buckets this unordered_map type could ever have.

The value is usually huge. Real programs run out of memory long before they reach it. Include <unordered_map> to use it.

This is a member of std::unordered_map<Key, T>, a hash table of key and value pairs. Each key is unique. Find, insert, and erase are usually average constant time. Unlike std::map, keys are not sorted and iteration order is unspecified. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.

Syntax

Call max_bucket_count with empty brackets.

C++
std::size_t limit = m.max_bucket_count();
bool ok = m.bucket_count() <= limit;
CallWhat it does
m.max_bucket_count()Returns the theoretical maximum bucket count.

Do not print the raw number in tests that must match everywhere. It is implementation-defined. Use it only as an upper bound check.

Library declarations
C++
size_type max_bucket_count() const;

Parameters

ParameterDescription
Nonemax_bucket_count() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
size_typeThe maximum number of buckets the type could support.

C++ Examples

Example 1 — max_bucket_count() Is Far Above Current Buckets

Show that the limit is larger than the current table.

C++
#include <iostream>
#include <string>
#include <unordered_map>

int main() {
    std::unordered_map<std::string, int> m = {{"a", 1}};
    std::cout << std::boolalpha;

    std::cout << "bucket_count: " << m.bucket_count() << '\n';
    std::cout << "max > current: "
              << (m.max_bucket_count() > m.bucket_count()) << '\n';
    return 0;
}

How It Works

  1. The map has a small current table, reported by bucket_count().
  2. max_bucket_count() returns a much larger library limit.
  3. The boolean check prints true. We avoid printing the huge limit itself so the lesson stays portable.

Example 2 — Limit Is Beyond Everyday Sizes

Check that the maximum is greater than one million.

C++
#include <iostream>
#include <unordered_map>

int main() {
    std::unordered_map<int, int> m;
    std::cout << std::boolalpha;
    std::cout << "max buckets over a million: "
              << (m.max_bucket_count() > 1000000) << '\n';
    std::cout << "pairs stored: " << m.size() << '\n';
    return 0;
}

How It Works

  1. Even an empty unordered_map type already has a max bucket limit.
  2. On common 64-bit libraries that limit is far above one million, so the check is true.
  3. size() is still 0. The limit and the stored count are separate ideas.

Example 3 — Refuse an Impossible Rehash

Skip rehash when the requested count is above the maximum.

C++
#include <iostream>
#include <unordered_map>

bool safeRehash(std::unordered_map<int, int>& m, std::size_t want) {
    if (want > m.max_bucket_count()) {
        return false;
    }
    m.rehash(want);
    return true;
}

int main() {
    std::unordered_map<int, int> m;
    std::cout << std::boolalpha;
    std::cout << "rehash 32: " << safeRehash(m, 32) << '\n';
    std::cout << "buckets: " << m.bucket_count() << '\n';
    return 0;
}

How It Works

  1. safeRehash compares the request with max_bucket_count() before calling rehash().
  2. A request of 32 is far below the limit, so the call succeeds and returns true.
  3. The library may round the bucket count up to a nearby size, so the printed count can be a little above 32.

Frequently Asked Questions

The theoretical maximum number of buckets for this unordered_map type.
No. max_size() limits elements (pairs). max_bucket_count() limits hash buckets.
No. That request is enormous and will usually fail. Reserve only what you need.
It comes from address size and allocator limits. On 64-bit systems it is typically huge.
No. The value is implementation-defined.

Next: max_load_factor()

Keep going through the std::unordered_map functions. Get or set the load factor limit that triggers growth.

max_load_factor() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful