|
| 1 | +--- |
| 2 | +Title: "max_bucket_count()" |
| 3 | +Description: "Returns the maximum number of buckets that the unordered set can have." |
| 4 | +Subjects: |
| 5 | + - "Code Foundations" |
| 6 | + - "Computer Science" |
| 7 | +Tags: |
| 8 | + - "Methods" |
| 9 | + - "Sets" |
| 10 | + - "Hash Tables" |
| 11 | + - "STL" |
| 12 | +CatalogContent: |
| 13 | + - "learn-c-plus-plus" |
| 14 | + - "paths/computer-science" |
| 15 | +--- |
| 16 | + |
| 17 | +The **`max_bucket_count()`** method returns the maximum number of buckets that an [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set) container can have. This value represents a theoretical upper limit imposed by the system or library implementation, not the current number of buckets in use. The actual number of buckets used by the container is typically much smaller and can be queried using `bucket_count()`. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +unordered_set_name.max_bucket_count(); |
| 23 | +``` |
| 24 | + |
| 25 | +**Return value:** |
| 26 | + |
| 27 | +Returns a `size_type` value representing the maximum possible number of buckets the container could theoretically hold. This is a system-dependent constant that is typically a very large number. |
| 28 | + |
| 29 | +## Example |
| 30 | + |
| 31 | +This example demonstrates obtaining the maximum bucket count and comparing it with the current bucket count: |
| 32 | + |
| 33 | +```cpp |
| 34 | +#include <iostream> |
| 35 | +#include <unordered_set> |
| 36 | +using namespace std; |
| 37 | + |
| 38 | +int main() { |
| 39 | + unordered_set<int> numbers = {10, 20, 30, 40, 50}; |
| 40 | + |
| 41 | + cout << "Maximum bucket count: " << numbers.max_bucket_count() << "\n"; |
| 42 | + cout << "Current bucket count: " << numbers.bucket_count() << "\n"; |
| 43 | + cout << "Number of elements: " << numbers.size() << "\n"; |
| 44 | + |
| 45 | + return 0; |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +A sample output of this code is: |
| 50 | + |
| 51 | +```shell |
| 52 | +Maximum bucket count: 576460752303423487 |
| 53 | +Current bucket count: 7 |
| 54 | +Number of elements: 5 |
| 55 | +``` |
| 56 | + |
| 57 | +> **Note:** The exact maximum bucket count value may vary depending on the system architecture and compiler implementation. |
| 58 | +
|
| 59 | +## Codebyte Example |
| 60 | + |
| 61 | +In this example, the code demonstrates how the maximum bucket count remains constant while the current bucket count can change as elements are added: |
| 62 | + |
| 63 | +```codebyte/cpp |
| 64 | +#include <iostream> |
| 65 | +#include <unordered_set> |
| 66 | +#include <string> |
| 67 | +using namespace std; |
| 68 | +
|
| 69 | +int main() { |
| 70 | + unordered_set<string> fruits = {"apple", "banana", "cherry"}; |
| 71 | +
|
| 72 | + cout << "Initial state:\n"; |
| 73 | + cout << "Max bucket count: " << fruits.max_bucket_count() << "\n"; |
| 74 | + cout << "Current bucket count: " << fruits.bucket_count() << "\n"; |
| 75 | + cout << "Current size: " << fruits.size() << "\n"; |
| 76 | +
|
| 77 | + // Add more elements |
| 78 | + fruits.insert("date"); |
| 79 | + fruits.insert("elderberry"); |
| 80 | + fruits.insert("fig"); |
| 81 | +
|
| 82 | + cout << "\nAfter adding more elements:\n"; |
| 83 | + cout << "Max bucket count: " << fruits.max_bucket_count() << "\n"; |
| 84 | + cout << "Current bucket count: " << fruits.bucket_count() << "\n"; |
| 85 | + cout << "Current size: " << fruits.size() << "\n"; |
| 86 | +
|
| 87 | + return 0; |
| 88 | +} |
| 89 | +``` |
0 commit comments