C++ std::unordered_set bucket_size() Function

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

Definition

In C++, std::unordered_set::bucket_size() tells you how many values 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_set> to use it.

This is a member of std::unordered_set<T>, a hash table of unique values. Find, insert, and erase are usually average constant time. Unlike std::set, values are not sorted and iteration order is unspecified. Elements are const, so you cannot change a stored value through an iterator. There is no operator[] and no at(). Prefer find() or contains() to look up a value, and call reserve() before many inserts.

Syntax

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

C++
std::size_t n = m.bucket_size(0);     // how many values 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_set>

int main() {
    std::unordered_set<std::string> 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_set>

int main() {
    std::unordered_set<std::string> 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 values are stored in the unordered_set.
  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_set>

int main() {
    std::unordered_set<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_set functions. Get a read-only iterator to one value (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.

10 people found this page helpful