C++ unordered_map bucket_count() Function
Example
Check if the number of buckets grows when you add 100 elements:
unordered_map<int, int> numbers;
size_t before = numbers.bucket_count();
for (int i = 0; i < 100; i++) {
numbers[i] = i * i;
}
if (numbers.bucket_count() > before) {
cout << "The number of buckets has grown";
}
Definition and Usage
The bucket_count() function returns the number of buckets in an unordered map.
A bucket is a group that holds elements. The unordered map calculates a number from each key (a hash), and uses it to choose the bucket for the element. When you add elements, the unordered map adds more buckets automatically, so that each bucket only holds a few elements.
The bucket_count function is defined in the <unordered_map> header file.
Note: The number of buckets depends on the compiler, so the example does not print it. It only checks that the number grows.
Syntax
unordered_map.bucket_count()
Parameter Values
None.
Technical Details
| Returns: | The number of buckets in the unordered map (a non-negative integer). |
|---|
Related Pages
Read more about unordered maps in our Unordered Map Tutorial.