From c62dda91e013ed72b14a48eb79b71930f924c6f6 Mon Sep 17 00:00:00 2001 From: Charles Hardy Date: Mon, 29 Dec 2025 16:17:07 +0000 Subject: [PATCH 1/8] Created bucket-size.md and populated this file with information regarding bucket-size method in C++ --- .../terms/bucket-size/bucket-size.md | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md diff --git a/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md b/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md new file mode 100644 index 00000000000..f65959bd315 --- /dev/null +++ b/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md @@ -0,0 +1,113 @@ +--- +Title: 'bucket_size()' # Required; the file name should be the same as the title, but lowercase, with dashes instead of spaces, and all punctuation removed +Description: 'Returns the number of elements stored in a specific bucket of an unordered_map.' # Required; ideally under 150 characters and starts with a present-tense verb (used in search engine results and content previews) +Subjects: # Please only use Subjects in the subjects.md file (https://github.com/Codecademy/docs/blob/main/documentation/subjects.md). If that list feels insufficient, feel free to create a new Subject and add it to subjects.md in your PR! + - 'Code Foundations' + - 'Computer Science' +Tags: # Please only use Tags in the tags.md file (https://github.com/Codecademy/docs/blob/main/documentation/tags.md). If that list feels insufficient, feel free to create a new Tag and add it to tags.md in your PR! + - 'Optimization' +CatalogContent: # Please use course/path landing page slugs, rather than linking to individual content items. If listing multiple items, please put the most relevant one first + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +The **.bucket_size()** method returns the number of elements stored in a specific bucket of an [`unordered_map](https://www.codecademy.com/resources/docs/cpp/unordered-map). In C++, an unordered_map uses a hash table internally where elements are distributed across multiple buckets based on their key’s hash value. This method helps analyze the distribution of elements and can be useful for performance optimization and understanding collision handling in the hash table. + +## Syntax + +```pseudo +unordered_map.bucket_size(n) +``` +**Parameters:** + +*n*: The bucket number to query. This value must be less than the total number of buckets returned by .bucket_count(). It is of type size_type, which is an unsigned integral type. +Return value: + +The .bucket_size() method returns the number of elements in bucket n as an unsigned integer of type size_type. + +## Example + +This example demonstrates how to use .bucket_size() to check the number of elements in each bucket of an unordered_map: + +``` +#include +#include +#include + +int main() { + // Create an unordered_map with string keys and integer values + std::unordered_map ages = { + {"Alice", 25}, + {"Bob", 30}, + {"Charlie", 35}, + {"Diana", 28} + }; + + // Get the total number of buckets + unsigned int total_buckets = ages.bucket_count(); + std::cout << "Total buckets: " << total_buckets << "\n\n"; + + // Display the number of elements in each bucket + for (unsigned int i = 0; i < total_buckets; i++) { + std::cout << "Bucket " << i << " has " << ages.bucket_size(i) << " elements\n"; + } + + return 0; +} +``` +This example results in the following output: +``` +Total buckets: 5 + +Bucket 0 has 1 elements +Bucket 1 has 1 elements +Bucket 2 has 2 elements +Bucket 3 has 0 elements +Bucket 4 has 0 elements + +``` +The output shows how elements are distributed across the buckets. Some buckets may be empty while others contain one or more elements depending on the hash function’s distribution. + +## Codebyte Example (if applicable) + +This example demonstrates using .bucket_size() to identify buckets with multiple elements, which indicates hash collisions in a user authentication system: + +```codebyte/js +#include +#include +#include + +int main() { + // Create a map storing user sessions with session IDs + std::unordered_map sessions = { + {"session_a1b2", "user_101"}, + {"session_c3d4", "user_102"}, + {"session_e5f6", "user_103"}, + {"session_g7h8", "user_104"}, + {"session_i9j0", "user_105"}, + {"session_k1l2", "user_106"} + }; + + unsigned int total_buckets = sessions.bucket_count(); + + std::cout << "Buckets with collisions (multiple elements):\n"; + + // Find and report buckets with more than one element + for (unsigned int i = 0; i < total_buckets; i++) { + unsigned int size = sessions.bucket_size(i); + + if (size > 1) { + std::cout << "Bucket " << i << " has " << size << " elements (collision detected)\n"; + + // Display which sessions are in this bucket + std::cout << " Sessions in this bucket: "; + for (auto it = sessions.begin(i); it != sessions.end(i); ++it) { + std::cout << it->first << " "; + } + std::cout << "\n"; + } + } + + return 0; +} +``` \ No newline at end of file From 53f03b1361d468005328a34d241c54530585ceb0 Mon Sep 17 00:00:00 2001 From: Charles Hardy Date: Mon, 29 Dec 2025 16:18:59 +0000 Subject: [PATCH 2/8] Created bucket-size.md and populated this file with information regarding bucket-size method in C++ --- .../terms/bucket-size/bucket-size.md | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md b/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md index f65959bd315..431d821b65b 100644 --- a/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md +++ b/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md @@ -1,17 +1,17 @@ --- -Title: 'bucket_size()' # Required; the file name should be the same as the title, but lowercase, with dashes instead of spaces, and all punctuation removed -Description: 'Returns the number of elements stored in a specific bucket of an unordered_map.' # Required; ideally under 150 characters and starts with a present-tense verb (used in search engine results and content previews) -Subjects: # Please only use Subjects in the subjects.md file (https://github.com/Codecademy/docs/blob/main/documentation/subjects.md). If that list feels insufficient, feel free to create a new Subject and add it to subjects.md in your PR! +Title: 'bucket_size()' +Description: 'Returns the number of elements stored in a specific bucket of an unordered_map.' +Subjects: - 'Code Foundations' - 'Computer Science' -Tags: # Please only use Tags in the tags.md file (https://github.com/Codecademy/docs/blob/main/documentation/tags.md). If that list feels insufficient, feel free to create a new Tag and add it to tags.md in your PR! +Tags: - 'Optimization' -CatalogContent: # Please use course/path landing page slugs, rather than linking to individual content items. If listing multiple items, please put the most relevant one first +CatalogContent: - 'learn-c-plus-plus' - 'paths/computer-science' --- -The **.bucket_size()** method returns the number of elements stored in a specific bucket of an [`unordered_map](https://www.codecademy.com/resources/docs/cpp/unordered-map). In C++, an unordered_map uses a hash table internally where elements are distributed across multiple buckets based on their key’s hash value. This method helps analyze the distribution of elements and can be useful for performance optimization and understanding collision handling in the hash table. +The **.bucket_size()** method returns the number of elements stored in a specific bucket of an [`unordered_map`](https://www.codecademy.com/resources/docs/cpp/unordered-map). In C++, an unordered_map uses a hash table internally where elements are distributed across multiple buckets based on their key’s hash value. This method helps analyze the distribution of elements and can be useful for performance optimization and understanding collision handling in the hash table. ## Syntax @@ -20,7 +20,7 @@ unordered_map.bucket_size(n) ``` **Parameters:** -*n*: The bucket number to query. This value must be less than the total number of buckets returned by .bucket_count(). It is of type size_type, which is an unsigned integral type. +n: The bucket number to query. This value must be less than the total number of buckets returned by .bucket_count(). It is of type size_type, which is an unsigned integral type. Return value: The .bucket_size() method returns the number of elements in bucket n as an unsigned integer of type size_type. @@ -29,8 +29,7 @@ The .bucket_size() method returns the number of elements in bucket n as an unsig This example demonstrates how to use .bucket_size() to check the number of elements in each bucket of an unordered_map: -``` -#include +```#include #include #include @@ -56,8 +55,7 @@ int main() { } ``` This example results in the following output: -``` -Total buckets: 5 +```Total buckets: 5 Bucket 0 has 1 elements Bucket 1 has 1 elements From 360d8d3e3fa84845ede5eba8cb9eb8995fa4acae Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 5 Jan 2026 15:46:57 +0530 Subject: [PATCH 3/8] Revise bucket_size() documentation for unordered_set Updated the documentation for the bucket_size() method to reflect its use with unordered_set instead of unordered_map. Adjusted examples and descriptions accordingly. --- .../terms/bucket-size/bucket-size.md | 113 ++++++++---------- 1 file changed, 48 insertions(+), 65 deletions(-) diff --git a/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md b/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md index 431d821b65b..699aed5e764 100644 --- a/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md +++ b/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md @@ -1,111 +1,94 @@ --- Title: 'bucket_size()' -Description: 'Returns the number of elements stored in a specific bucket of an unordered_map.' +Description: 'Returns the number of elements stored in a specific bucket of an unordered_set.' Subjects: - 'Code Foundations' - 'Computer Science' Tags: - - 'Optimization' + - 'Containers' + - 'Sets' + - 'STL' CatalogContent: - 'learn-c-plus-plus' - 'paths/computer-science' --- -The **.bucket_size()** method returns the number of elements stored in a specific bucket of an [`unordered_map`](https://www.codecademy.com/resources/docs/cpp/unordered-map). In C++, an unordered_map uses a hash table internally where elements are distributed across multiple buckets based on their key’s hash value. This method helps analyze the distribution of elements and can be useful for performance optimization and understanding collision handling in the hash table. +The **`bucket_size()`** method returns the number of elements stored in a specific bucket of an [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set). An `unordered_set` stores elements in a hash table, where values are distributed into buckets based on their hash. This method is useful for inspecting bucket distribution and understanding hash collisions. + ## Syntax ```pseudo -unordered_map.bucket_size(n) +unordered_set_name.bucket_size(n) ``` + **Parameters:** -n: The bucket number to query. This value must be less than the total number of buckets returned by .bucket_count(). It is of type size_type, which is an unsigned integral type. -Return value: +n: The bucket number to query. This value must be less than the total number of buckets returned by `bucket_count()`. It is of type `size_type`, which is an unsigned integral type. + +**Return value:** -The .bucket_size() method returns the number of elements in bucket n as an unsigned integer of type size_type. +The `bucket_size()` method returns the number of elements in bucket `n` as an unsigned integer of type `size_type`. ## Example -This example demonstrates how to use .bucket_size() to check the number of elements in each bucket of an unordered_map: +This example shows how to inspect how elements are distributed across buckets in an `unordered_set`: -```#include -#include -#include +```cpp +#include +#include int main() { - // Create an unordered_map with string keys and integer values - std::unordered_map ages = { - {"Alice", 25}, - {"Bob", 30}, - {"Charlie", 35}, - {"Diana", 28} - }; - - // Get the total number of buckets - unsigned int total_buckets = ages.bucket_count(); - std::cout << "Total buckets: " << total_buckets << "\n\n"; - - // Display the number of elements in each bucket - for (unsigned int i = 0; i < total_buckets; i++) { - std::cout << "Bucket " << i << " has " << ages.bucket_size(i) << " elements\n"; + std::unordered_set numbers = {10, 20, 30, 40, 50}; + + std::size_t totalBuckets = numbers.bucket_count(); + std::cout << "Total buckets: " << totalBuckets << "\n"; + + for (std::size_t i = 0; i < totalBuckets; i++) { + std::cout << "Bucket " << i + << " has " << numbers.bucket_size(i) + << " elements\n"; } return 0; } ``` This example results in the following output: -```Total buckets: 5 -Bucket 0 has 1 elements -Bucket 1 has 1 elements -Bucket 2 has 2 elements +```shell +Total buckets: 5 +Bucket 0 has 5 elements +Bucket 1 has 0 elements +Bucket 2 has 0 elements Bucket 3 has 0 elements Bucket 4 has 0 elements - ``` -The output shows how elements are distributed across the buckets. Some buckets may be empty while others contain one or more elements depending on the hash function’s distribution. -## Codebyte Example (if applicable) +This output shows how many elements are placed in each bucket. Some buckets may be empty, while others may contain multiple elements depending on the hash distribution. + +## Codebyte Example -This example demonstrates using .bucket_size() to identify buckets with multiple elements, which indicates hash collisions in a user authentication system: +This example demonstrates using `bucket_size()` to detect hash collisions by finding buckets that contain more than one element: -```codebyte/js +```codebyte/cpp #include -#include -#include +#include int main() { - // Create a map storing user sessions with session IDs - std::unordered_map sessions = { - {"session_a1b2", "user_101"}, - {"session_c3d4", "user_102"}, - {"session_e5f6", "user_103"}, - {"session_g7h8", "user_104"}, - {"session_i9j0", "user_105"}, - {"session_k1l2", "user_106"} - }; - - unsigned int total_buckets = sessions.bucket_count(); - - std::cout << "Buckets with collisions (multiple elements):\n"; - - // Find and report buckets with more than one element - for (unsigned int i = 0; i < total_buckets; i++) { - unsigned int size = sessions.bucket_size(i); - - if (size > 1) { - std::cout << "Bucket " << i << " has " << size << " elements (collision detected)\n"; - - // Display which sessions are in this bucket - std::cout << " Sessions in this bucket: "; - for (auto it = sessions.begin(i); it != sessions.end(i); ++it) { - std::cout << it->first << " "; - } - std::cout << "\n"; + std::unordered_set values = {1, 2, 3, 4, 5, 6, 7, 8}; + + std::size_t totalBuckets = values.bucket_count(); + + std::cout << "Buckets with collisions:\n"; + + for (std::size_t i = 0; i < totalBuckets; i++) { + if (values.bucket_size(i) > 1) { + std::cout << "Bucket " << i + << " has " << values.bucket_size(i) + << " elements\n"; } } return 0; } -``` \ No newline at end of file +``` From 4f9dcdb57b85e4e68c621d1c9b7e5ee62cc8d14f Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 7 Jan 2026 19:39:59 +0530 Subject: [PATCH 4/8] No need for underscore in desc --- .../cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md b/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md index 699aed5e764..d3121a04778 100644 --- a/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md +++ b/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md @@ -1,6 +1,6 @@ --- Title: 'bucket_size()' -Description: 'Returns the number of elements stored in a specific bucket of an unordered_set.' +Description: 'Returns the number of elements stored in a specific bucket of an unordered set.' Subjects: - 'Code Foundations' - 'Computer Science' From 559985755a22d23f1e49badbbe17dab1681f1964 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 7 Jan 2026 19:40:49 +0530 Subject: [PATCH 5/8] Update bucket-size.md --- .../cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md b/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md index d3121a04778..13ea0f249ea 100644 --- a/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md +++ b/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md @@ -13,7 +13,7 @@ CatalogContent: - 'paths/computer-science' --- -The **`bucket_size()`** method returns the number of elements stored in a specific bucket of an [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set). An `unordered_set` stores elements in a hash table, where values are distributed into buckets based on their hash. This method is useful for inspecting bucket distribution and understanding hash collisions. +The **`bucket_size()`** method returns the number of elements stored in a specific bucket of an [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set). An `unordered_set` stores elements in a [hash table](https://www.codecademy.com/resources/docs/general/data-structures/hash-table), where values are distributed into buckets based on their hash. This method is useful for inspecting bucket distribution and understanding hash collisions. ## Syntax From 96678f1997c0f617019bd32ebfa60a5dbac4d29d Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 7 Jan 2026 19:41:28 +0530 Subject: [PATCH 6/8] Update bucket-size.md --- .../cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md b/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md index 13ea0f249ea..2bdc31947f8 100644 --- a/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md +++ b/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md @@ -24,7 +24,7 @@ unordered_set_name.bucket_size(n) **Parameters:** -n: The bucket number to query. This value must be less than the total number of buckets returned by `bucket_count()`. It is of type `size_type`, which is an unsigned integral type. +`n`: The bucket number to query. This value must be less than the total number of buckets returned by `bucket_count()`. It is of type `size_type`, which is an unsigned integral type. **Return value:** From fae72f48b0098093cb30bbdc08ad0e8ad41355f0 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 7 Jan 2026 19:43:19 +0530 Subject: [PATCH 7/8] Update bucket-size.md --- .../terms/bucket-size/bucket-size.md | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md b/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md index 2bdc31947f8..e7876985d7f 100644 --- a/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md +++ b/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md @@ -13,7 +13,7 @@ CatalogContent: - 'paths/computer-science' --- -The **`bucket_size()`** method returns the number of elements stored in a specific bucket of an [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set). An `unordered_set` stores elements in a [hash table](https://www.codecademy.com/resources/docs/general/data-structures/hash-table), where values are distributed into buckets based on their hash. This method is useful for inspecting bucket distribution and understanding hash collisions. +The **`bucket_size()`** method returns the number of elements stored in a specific bucket of an [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set). An `unordered_set` stores elements in a [hash table](https://www.codecademy.com/resources/docs/general/data-structures/hash-table), where values are distributed into buckets based on their hash. This method helps inspect bucket distribution and understanding hash collisions. ## Syntax @@ -38,21 +38,24 @@ This example shows how to inspect how elements are distributed across buckets in #include #include +using namespace std; + int main() { - std::unordered_set numbers = {10, 20, 30, 40, 50}; + unordered_set numbers = {10, 20, 30, 40, 50}; - std::size_t totalBuckets = numbers.bucket_count(); - std::cout << "Total buckets: " << totalBuckets << "\n"; + size_t totalBuckets = numbers.bucket_count(); + cout << "Total buckets: " << totalBuckets << "\n"; - for (std::size_t i = 0; i < totalBuckets; i++) { - std::cout << "Bucket " << i - << " has " << numbers.bucket_size(i) - << " elements\n"; + for (size_t i = 0; i < totalBuckets; i++) { + cout << "Bucket " << i + << " has " << numbers.bucket_size(i) + << " elements\n"; } return 0; } ``` + This example results in the following output: ```shell @@ -64,7 +67,7 @@ Bucket 3 has 0 elements Bucket 4 has 0 elements ``` -This output shows how many elements are placed in each bucket. Some buckets may be empty, while others may contain multiple elements depending on the hash distribution. +This output displays the number of elements assigned to each bucket. Some buckets may be empty, while others may contain multiple elements depending on the hash distribution. ## Codebyte Example @@ -74,18 +77,19 @@ This example demonstrates using `bucket_size()` to detect hash collisions by fin #include #include -int main() { - std::unordered_set values = {1, 2, 3, 4, 5, 6, 7, 8}; +using namespace std; - std::size_t totalBuckets = values.bucket_count(); +int main() { + unordered_set values = {1, 2, 3, 4, 5, 6, 7, 8}; - std::cout << "Buckets with collisions:\n"; + size_t totalBuckets = values.bucket_count(); + cout << "Buckets with collisions:\n"; - for (std::size_t i = 0; i < totalBuckets; i++) { + for (size_t i = 0; i < totalBuckets; i++) { if (values.bucket_size(i) > 1) { - std::cout << "Bucket " << i - << " has " << values.bucket_size(i) - << " elements\n"; + cout << "Bucket " << i + << " has " << values.bucket_size(i) + << " elements\n"; } } From 10208b6848ab4398b67eda8d5c970e1bc4acb1e1 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 7 Jan 2026 19:48:32 +0530 Subject: [PATCH 8/8] lint fix --- .../cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md | 1 - 1 file changed, 1 deletion(-) diff --git a/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md b/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md index e7876985d7f..49b4dde995b 100644 --- a/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md +++ b/content/cpp/concepts/unordered-set/terms/bucket-size/bucket-size.md @@ -15,7 +15,6 @@ CatalogContent: The **`bucket_size()`** method returns the number of elements stored in a specific bucket of an [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set). An `unordered_set` stores elements in a [hash table](https://www.codecademy.com/resources/docs/general/data-structures/hash-table), where values are distributed into buckets based on their hash. This method helps inspect bucket distribution and understanding hash collisions. - ## Syntax ```pseudo