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;
Call
What 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
Parameter
Description
key
The key whose bucket index you want.
Return Value
Return Value
Description
size_type
A 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;
}
Output
Raj bucket: 4
in range: true
How It Works
Two names are stored in the unordered_set.
bucket("Raj") hashes the value and maps it into a bucket index.
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;
}
Output
size before: 1
kiwi would use bucket: 7
size after: 1
How It Works
The unordered_set starts with one value.
bucket("kiwi") only computes a slot. It does not add "kiwi".
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;
}
Output
bucket index: 2
elements in that bucket: 1
How It Works
bucket("b") finds the slot for key "b".
bucket_size(b) counts how many values share that slot.
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.