C++ std::unordered_set bucket() Function

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

Definition

In C++, std::unordered_set::bucket() answers: which bucket does this key belong to?

It hashes the value and maps that hash into the range [0, bucket_count()). You can ask about a value that is not in the unordered_set yet. 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 any key of the unordered_set's key type.

C++
std::size_t b = m.bucket("Raj");
std::cout << "Raj is in bucket " << b;
CallWhat it does
m.bucket(key)Returns the bucket index for key with the current table size.

After rehash() or a table growth, the same key can move to a different bucket index, because the index depends on bucket_count().

Library declarations
C++
size_type bucket(const Key& key) const;

Parameters

ParameterDescription
keyThe key whose bucket index you want.

Return Value

Return ValueDescription
size_typeA bucket index in [0, bucket_count()).

C++ Examples

Example 1 — Find the Bucket for a Key

Insert keys and print the bucket index for one of them.

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

int main() {
    std::unordered_set<std::string> age = {{"Mia", 21}, {"Raj", 25}};

    std::size_t b = age.bucket("Raj");
    std::cout << "Raj bucket: " << b << '\n';
    std::cout << "in range: " << std::boolalpha
              << (b < age.bucket_count()) << '\n';
    return 0;
}

How It Works

  1. Two names are stored in the unordered_set.
  2. bucket("Raj") hashes the value and maps it into a bucket index.
  3. The index is always less than bucket_count(), which the boolean check confirms.

Example 2 — Bucket of a Key Not Yet Inserted

Ask for the bucket of a value that is not in the unordered_set.

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

int main() {
    std::unordered_set<std::string> m = {{"apple", 5}};

    std::cout << "size before: " << m.size() << '\n';
    std::size_t b = m.bucket("kiwi");
    std::cout << "kiwi would use bucket: " << b << '\n';
    std::cout << "size after: " << m.size() << '\n';
    return 0;
}

How It Works

  1. The unordered_set starts with one value.
  2. bucket("kiwi") only computes a slot. It does not add "kiwi".
  3. The size stays 1, which proves the call is a pure query.

Example 3 — How Full Is This Key's Bucket?

Use bucket() and bucket_size() together.

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

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

    std::size_t b = m.bucket("b");
    std::cout << "bucket index: " << b << '\n';
    std::cout << "elements in that bucket: " << m.bucket_size(b) << '\n';
    return 0;
}

How It Works

  1. bucket("b") finds the slot for key "b".
  2. bucket_size(b) counts how many values share that slot.
  3. Here the bucket holds one element (exact index can vary by library/hash).

Frequently Asked Questions

It returns the bucket index where that key belongs with the current table.
No. It only computes the index.
Yes. After rehash or automatic growth, bucket_count changes, so indexes can change.
Use begin(n)/end(n) local iterators for bucket n (advanced). Beginners usually just need bucket() and bucket_size().
Not reliably. Hashing and bucket counts are implementation-defined.

Next: bucket_count()

Keep going through the std::unordered_set functions. How many hash buckets the unordered_set currently has.

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

8 people found this page helpful