C++ std::unordered_map bucket_size() Function

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

Definition

In C++, std::unordered_map::bucket_size() tells you how many pairs live in one bucket.

If many keys hash to the same bucket, that bucket grows and lookups there get slower. Watching bucket sizes helps you understand collisions. 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

Pass a bucket index from 0 to bucket_count() - 1.

C++
std::size_t n = m.bucket_size(0);     // how many pairs in bucket 0
for (std::size_t b = 0; b < m.bucket_count(); ++b) {
    // m.bucket_size(b)
}
CallWhat it does
m.bucket_size(n)Returns the number of elements currently stored in bucket n.

The index n must be less than bucket_count(). Passing an out-of-range index is undefined behavior.

Library declarations
C++
size_type bucket_size(size_type n) const;

Parameters

ParameterDescription
nBucket index in the range [0, bucket_count()).

Return Value

Return ValueDescription
size_typeHow many elements are in that bucket. 0 means empty.

C++ Examples

Example 1 — Check Bucket 0

Print the size of bucket 0 after inserting a few keys.

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

int main() {
    std::unordered_map<std::string, int> m = {{"red", 1}, {"blue", 2}};

    std::cout << "bucket_count: " << m.bucket_count() << '\n';
    std::cout << "bucket_size(0): " << m.bucket_size(0) << '\n';
    return 0;
}

How It Works

  1. Two keys are inserted into the hash table.
  2. bucket_count() shows how many slots exist.
  3. Bucket 0 happens to be empty in this run, so bucket_size(0) is 0. Other buckets may hold the keys.

Example 2 — Sum of Bucket Sizes Equals size()

Walk every bucket and add the sizes.

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

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

    std::size_t total = 0;
    for (std::size_t b = 0; b < m.bucket_count(); ++b) {
        total += m.bucket_size(b);
    }

    std::cout << "size: " << m.size() << '\n';
    std::cout << "sum of bucket sizes: " << total << '\n';
    return 0;
}

How It Works

  1. Four pairs are stored in the unordered_map.
  2. The loop visits every bucket index and adds bucket_size(b).
  3. The sum equals size(), because every element lives in exactly one bucket.

Example 3 — Largest Bucket Size

Scan buckets to find the maximum chain length.

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

int main() {
    std::unordered_map<int, int> m;
    for (int i = 0; i < 20; ++i) {
        m[i] = i;
    }

    std::size_t hottest = 0;
    for (std::size_t b = 0; b < m.bucket_count(); ++b) {
        hottest = std::max(hottest, m.bucket_size(b));
    }

    std::cout << "elements: " << m.size() << '\n';
    std::cout << "max bucket_size: " << hottest << '\n';
    return 0;
}

How It Works

  1. Twenty keys are inserted. The hash function spreads them across the table.
  2. Each bucket size is compared so hottest keeps the largest one.
  3. A small max bucket size means collisions are mild. Exact values depend on the hash and the library.

Frequently Asked Questions

The number of elements stored in bucket n.
Yes. That means several keys hashed into the same bucket (collisions).
0 through bucket_count() - 1. Out-of-range indexes are undefined behavior.
Yes. Rehash moves elements into a new set of buckets, so sizes change.
Yes. Many buckets are often empty in a healthy table.

Next: cbegin()

Keep going through the std::unordered_map functions. Get a read-only iterator to the first pair (C++11).

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

11 people found this page helpful