In C++, std::unordered_map::bucket() answers: which bucket does this key belong to?
It hashes the key and maps that hash into the range [0, bucket_count()). You can ask about a key that is not in the map yet. 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 any key of the map'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_map>
int main() {
std::unordered_map<std::string, int> 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_map.
bucket("Raj") hashes the key 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 key that is not in the map.
C++
#include <iostream>
#include <string>
#include <unordered_map>
int main() {
std::unordered_map<std::string, int> 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 map starts with one pair.
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_map>
int main() {
std::unordered_map<std::string, int> 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 pairs 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.