C++ std::unordered_map bucket_count() Function

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

Definition

In C++, std::unordered_map::bucket_count() returns how many buckets the hash table has right now.

Think of buckets as a row of boxes. A hash function turns each key into a number, and that number picks a box. Several keys can share a box; that is called a collision. 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 bucket_count with empty brackets.

C++
std::size_t n = m.bucket_count();   // current number of buckets
std::cout << n;
CallWhat it does
m.bucket_count()Returns the current number of buckets as size_type.

The count can grow when you insert many keys, or when you call rehash() / reserve(). An empty unordered_map may still have some buckets ready.

Library declarations
C++
size_type bucket_count() const;

Parameters

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

Return Value

Return ValueDescription
size_typeThe number of buckets in the hash table right now.

C++ Examples

Example 1 — bucket_count() After Creating a Map

Print size and bucket_count for a small unordered_map.

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

int main() {
    std::unordered_map<std::string, int> m = {{"a", 1}, {"b", 2}, {"c", 3}};

    std::cout << std::boolalpha;
    std::cout << "size: " << m.size() << '\n';
    std::cout << "has buckets: " << (m.bucket_count() > 0) << '\n';
    std::cout << "buckets >= size: " << (m.bucket_count() >= m.size()) << '\n';
    return 0;
}

How It Works

  1. Three pairs are stored, so size() is 3.
  2. bucket_count() > 0 is true, so the table has a real bucket array. Exact counts differ by library, so the demo prints booleans.
  3. The last check shows there are at least as many buckets as keys in this small example.

Example 2 — Bucket Count After Many Inserts

Insert many keys and show that the bucket count can rise.

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

int main() {
    std::unordered_map<int, int> m;
    std::size_t before = m.bucket_count();

    for (int i = 0; i < 100; ++i) {
        m[i] = i * 10;
    }

    std::cout << "before inserts: " << before << '\n';
    std::cout << "after inserts:  " << m.bucket_count() << '\n';
    std::cout << "size: " << m.size() << '\n';
    return 0;
}

How It Works

  1. A new unordered_map starts with a small table. before stores that early bucket count.
  2. One hundred inserts may force the table to grow so the load factor stays under the maximum.
  3. After the loop, size() is 100 and bucket_count() is larger than at the start. Exact numbers can vary by library; the key idea is that the table can expand.

Example 3 — bucket_count() Versus size()

Clear the map and show that buckets can remain while size is zero.

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

int main() {
    std::unordered_map<std::string, int> m = {{"x", 1}, {"y", 2}};
    std::cout << "filled size=" << m.size()
              << " buckets=" << m.bucket_count() << '\n';

    m.clear();
    std::cout << "cleared size=" << m.size()
              << " buckets=" << m.bucket_count() << '\n';
    return 0;
}

How It Works

  1. While the map holds two pairs, both size() and bucket_count() are positive.
  2. clear() removes every pair, so size() becomes 0.
  3. The bucket array can stay allocated. That is why bucket_count() may still be non-zero after a clear.

Frequently Asked Questions

The current number of hash buckets in the table. It is not the number of keys.
Libraries often keep extra buckets so keys spread out and collisions stay rare. The table may also grow in jumps.
Not necessarily. clear() removes elements; the bucket array can remain for later inserts.
Call rehash(n) to request at least n buckets, or reserve(n) to prepare for about n elements.
No. The starting size and growth policy are implementation-defined. Compare size and load_factor in portable code.

Next: bucket_size()

Keep going through the std::unordered_map functions. How many elements sit in one hash bucket.

bucket_size() 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.

10 people found this page helpful