C++ unordered_set bucket_count() Function
Example
Check if the number of buckets grows when you add 100 elements:
unordered_set<int> numbers;
size_t before = numbers.bucket_count();
for (int i = 0; i < 100; i++) {
numbers.insert(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 set.
A bucket is a group that holds elements. The unordered set calculates a number from each value (a hash), and uses it to choose the bucket for the element. When you add elements, the unordered set adds more buckets automatically, so that each bucket only holds a few elements.
The bucket_count function is defined in the <unordered_set> 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_set.bucket_count()
Parameter Values
None.
Technical Details
| Returns: | The number of buckets in the unordered set (a non-negative integer). |
|---|
Related Pages
Read more about unordered sets in our Unordered Set Tutorial.