C++ std::unordered_set bucket_count() Function

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

Definition

In C++, std::unordered_set::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 value into a number, and that number picks a box. Several values can share a box; that is called a collision. 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

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 values, or when you call rehash() / reserve(). An empty unordered_set 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 Set

Print size and bucket_count for a small unordered_set.

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

int main() {
    std::unordered_set<std::string> m = {"a", "b", "c"};

    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 values 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_set>

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

    for (int i = 0; i < 100; ++i) {
        m.insert(i);
    }

    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_set 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 value idea is that the table can expand.

Example 3 — bucket_count() Versus size()

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

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

int main() {
    std::unordered_set<std::string> m = {"x", "y"};
    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 unordered_set holds two values, both size() and bucket_count() are positive.
  2. clear() removes every value, 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 values.
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_set 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.

9 people found this page helpful