From 751a431b5ee41ae04c5a563e12ceff2bc70c3db4 Mon Sep 17 00:00:00 2001 From: Rishabh Ranjan Singh Date: Wed, 26 Nov 2025 11:29:22 +0530 Subject: [PATCH 1/7] docs: Add C++ unordered_set::cbegin() term entry (#8031) --- .../unordered-set/terms/cbegin/cbegin.md | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md diff --git a/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md b/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md new file mode 100644 index 00000000000..9e45f0fa9f4 --- /dev/null +++ b/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md @@ -0,0 +1,99 @@ +--- +Title: '.cbegin()' +Description: 'Returns a constant iterator pointing to the first element of the unordered_set.' +Subjects: + - 'Computer Science' + - 'Programming' +Tags: + - 'C++' + - 'Unordered Set' + - 'STL' + - 'Iterators' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +The **`.cbegin()`** method returns a **constant iterator** that points to the first element in an `std::unordered_set`. + +The key feature of a constant iterator (`const_iterator`) is that it allows you to **read** the elements but **prevents** you from modifying them. This is a best practice when iterating over a set to ensure data integrity. Since `unordered_set` does not guarantee any specific order, the element returned by `cbegin()` depends on the internal hash table structure. + +## Syntax + +The `.cbegin()` method is called directly on the `unordered_set` object. + +```cpp +unordered_set_name.cbegin(); +``` + +## Parameters + +The method takes no parameters. + +## Return Value + +Returns a `const_iterator` (constant iterator) pointing to the first element in the `unordered_set`. + +## Example + +This example demonstrates obtaining the starting element of an `unordered_set` using `cbegin()`. Note that attempting to modify the element pointed to by the `const_iterator` would result in a compilation error. + +```cpp +#include +#include +#include +#include // Required for std::find or general use + +int main() { + std::unordered_set unique_numbers = {10, 5, 20, 15}; + + // Get a constant iterator to the beginning + auto it = unique_numbers.cbegin(); + + // Print the value of the first element (read-only access) + std::cout << "The first element in the set's internal order is: " << *it << "\n"; + + // Advance the iterator to the next element + // Note: The order of elements is not guaranteed! + ++it; + + std::cout << "The second element is: " << *it << "\n"; + + // The following line would cause a compile-time error: + // *it = 99; + + return 0; +} +``` + +Output: + +``` +The first element in the set's internal order is: 20 +The second element is: 5 +``` + +## Codebyte + +Use the Codebyte below to access the first element of an `unordered_set` using the constant iterator returned by `.cbegin()`. + +```cpp +#include +#include + +int main() { + std::unordered_set colors = {"red", "green", "blue", "yellow"}; + + // Use cbegin() to get the constant iterator + auto start_it = colors.cbegin(); + + // Print the value the iterator points to + std::cout << "First element: " << *start_it << "\n"; + + // Advance the iterator + ++start_it; + std::cout << "Second element: " << *start_it << "\n"; + + return 0; +} +``` \ No newline at end of file From 6d9ab864618063268b17052a6ddf47d46bf78ff4 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Wed, 26 Nov 2025 16:23:43 +0530 Subject: [PATCH 2/7] content fixes --- .../unordered-set/terms/cbegin/cbegin.md | 95 ++++++++++--------- 1 file changed, 49 insertions(+), 46 deletions(-) diff --git a/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md b/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md index 9e45f0fa9f4..913ef42a3ad 100644 --- a/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md +++ b/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md @@ -1,99 +1,102 @@ --- -Title: '.cbegin()' -Description: 'Returns a constant iterator pointing to the first element of the unordered_set.' +Title: 'cbegin()' +Description: 'Returns a constant iterator pointing to either the first element of the unordered_set or the first element in a specific bucket.' Subjects: + - 'Code Foundations' - 'Computer Science' - - 'Programming' Tags: - - 'C++' - - 'Unordered Set' - - 'STL' - 'Iterators' + - 'Sets' + - 'STL' CatalogContent: - 'learn-c-plus-plus' - 'paths/computer-science' --- -The **`.cbegin()`** method returns a **constant iterator** that points to the first element in an `std::unordered_set`. - -The key feature of a constant iterator (`const_iterator`) is that it allows you to **read** the elements but **prevents** you from modifying them. This is a best practice when iterating over a set to ensure data integrity. Since `unordered_set` does not guarantee any specific order, the element returned by `cbegin()` depends on the internal hash table structure. +The **`cbegin()`** method returns a constant iterator that points to the first element of an `std::unordered_set`. A constant iterator allows read-only access to elements and prevents modification. Because `unordered_set` does not maintain any defined order, the element returned by `cbegin()` depends on its internal hash table structure. ## Syntax -The `.cbegin()` method is called directly on the `unordered_set` object. +```pseudo +unordered_set_name.cbegin(n); +``` -```cpp -unordered_set_name.cbegin(); +**Return value:** + +Returns a `const_iterator` (constant iterator) pointing to the first element in the `unordered_set`. + +Or, alternatively: + +```pseudo +unordered_set_name.cbegin(n); ``` -## Parameters +**Parameters:** -The method takes no parameters. +- `n` (size_type): The bucket index. Must be less than `bucket_count()`. -## Return Value +**Return value:** -Returns a `const_iterator` (constant iterator) pointing to the first element in the `unordered_set`. +A `const_local_iterator` pointing to the first element in bucket `n`. If the bucket is empty, the returned iterator equals `cend(n)`. ## Example -This example demonstrates obtaining the starting element of an `unordered_set` using `cbegin()`. Note that attempting to modify the element pointed to by the `const_iterator` would result in a compilation error. +This example demonstrates obtaining the starting element of an `unordered_set` using `cbegin()`: ```cpp #include #include #include -#include // Required for std::find or general use int main() { std::unordered_set unique_numbers = {10, 5, 20, 15}; - - // Get a constant iterator to the beginning - auto it = unique_numbers.cbegin(); - // Print the value of the first element (read-only access) - std::cout << "The first element in the set's internal order is: " << *it << "\n"; + auto it = unique_numbers.cbegin(); - // Advance the iterator to the next element - // Note: The order of elements is not guaranteed! - ++it; + std::cout << "The first element in internal order is: " << *it << "\n"; - std::cout << "The second element is: " << *it << "\n"; + ++it; + if (it != unique_numbers.cend()) { + std::cout << "The second element is: " << *it << "\n"; + } - // The following line would cause a compile-time error: - // *it = 99; + // *it = 99; // Error: cannot modify through const_iterator return 0; } ``` -Output: +A sample output of this code is: -``` +```shell The first element in the set's internal order is: 20 The second element is: 5 ``` -## Codebyte +> **Note:** Attempting to modify the element pointed to by the `const_iterator` would result in a compilation error. -Use the Codebyte below to access the first element of an `unordered_set` using the constant iterator returned by `.cbegin()`. +## Codebyte Example -```cpp +In this example, the code retrieves a constant iterator for a specific bucket in the unordered_set and prints all elements stored in that bucket: + +```codebyte/cpp #include #include int main() { - std::unordered_set colors = {"red", "green", "blue", "yellow"}; - - // Use cbegin() to get the constant iterator - auto start_it = colors.cbegin(); - - // Print the value the iterator points to - std::cout << "First element: " << *start_it << "\n"; - - // Advance the iterator - ++start_it; - std::cout << "Second element: " << *start_it << "\n"; + std::unordered_set words = {"cat", "dog", "rabbit", "lion"}; + + size_t bucket = 0; + + auto it = words.cbegin(bucket); + auto end = words.cend(bucket); + + std::cout << "Elements in bucket " << bucket << ":\n"; + + for (; it != end; ++it) { + std::cout << " " << *it << "\n"; + } return 0; } -``` \ No newline at end of file +``` From 4c04a115f2b0c4137b44b514e3368d750da471cb Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 3 Dec 2025 15:42:56 +0530 Subject: [PATCH 3/7] Update cbegin.md --- content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md b/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md index 913ef42a3ad..280d75ed6de 100644 --- a/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md +++ b/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md @@ -13,7 +13,7 @@ CatalogContent: - 'paths/computer-science' --- -The **`cbegin()`** method returns a constant iterator that points to the first element of an `std::unordered_set`. A constant iterator allows read-only access to elements and prevents modification. Because `unordered_set` does not maintain any defined order, the element returned by `cbegin()` depends on its internal hash table structure. +The **`cbegin()`** method returns a constant iterator that points to the first element of an [`std::unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set). A constant iterator allows read-only access to elements and prevents modification. Because `unordered_set` does not maintain any defined order, the element returned by `cbegin()` depends on its internal hash table structure. ## Syntax From f6555bff0c925103a1cb71ddd99fc666ee8b5ae3 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 3 Dec 2025 16:39:30 +0530 Subject: [PATCH 4/7] Update cbegin.md --- content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md b/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md index 280d75ed6de..b1a3fb89663 100644 --- a/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md +++ b/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md @@ -1,6 +1,6 @@ --- Title: 'cbegin()' -Description: 'Returns a constant iterator pointing to either the first element of the unordered_set or the first element in a specific bucket.' +Description: 'Returns a constant iterator pointing to either the first element of the unordered set or the first element in a specific bucket.' Subjects: - 'Code Foundations' - 'Computer Science' From 023bd0668896248524c30e2b80e384a88ec39f1c Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 3 Dec 2025 16:40:34 +0530 Subject: [PATCH 5/7] Remove std:: prefix in examples and explanations --- .../unordered-set/terms/cbegin/cbegin.md | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md b/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md index b1a3fb89663..4c6cfc5f462 100644 --- a/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md +++ b/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md @@ -13,13 +13,13 @@ CatalogContent: - 'paths/computer-science' --- -The **`cbegin()`** method returns a constant iterator that points to the first element of an [`std::unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set). A constant iterator allows read-only access to elements and prevents modification. Because `unordered_set` does not maintain any defined order, the element returned by `cbegin()` depends on its internal hash table structure. +The **`cbegin()`** method returns a constant iterator that points to the first element of an [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set). A constant iterator allows read-only access to elements and prevents modification. Because `unordered_set` does not maintain any defined order, the element returned by `cbegin()` depends on its internal hash table structure. ## Syntax ```pseudo unordered_set_name.cbegin(n); -``` +```` **Return value:** @@ -33,7 +33,7 @@ unordered_set_name.cbegin(n); **Parameters:** -- `n` (size_type): The bucket index. Must be less than `bucket_count()`. +* `n` (size_type): The bucket index. Must be less than `bucket_count()`. **Return value:** @@ -47,17 +47,18 @@ This example demonstrates obtaining the starting element of an `unordered_set` u #include #include #include +using namespace std; int main() { - std::unordered_set unique_numbers = {10, 5, 20, 15}; + unordered_set unique_numbers = {10, 5, 20, 15}; auto it = unique_numbers.cbegin(); - std::cout << "The first element in internal order is: " << *it << "\n"; + cout << "The first element in internal order is: " << *it << "\n"; ++it; if (it != unique_numbers.cend()) { - std::cout << "The second element is: " << *it << "\n"; + cout << "The second element is: " << *it << "\n"; } // *it = 99; // Error: cannot modify through const_iterator @@ -82,19 +83,20 @@ In this example, the code retrieves a constant iterator for a specific bucket in ```codebyte/cpp #include #include +using namespace std; int main() { - std::unordered_set words = {"cat", "dog", "rabbit", "lion"}; + unordered_set words = {"cat", "dog", "rabbit", "lion"}; size_t bucket = 0; auto it = words.cbegin(bucket); auto end = words.cend(bucket); - std::cout << "Elements in bucket " << bucket << ":\n"; + cout << "Elements in bucket " << bucket << ":\n"; for (; it != end; ++it) { - std::cout << " " << *it << "\n"; + cout << " " << *it << "\n"; } return 0; From ba01673a1d851b6e1636518153a21845a62d4787 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 3 Dec 2025 16:59:10 +0530 Subject: [PATCH 6/7] Update cbegin.md --- content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md b/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md index 4c6cfc5f462..afb6e078b78 100644 --- a/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md +++ b/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md @@ -78,7 +78,7 @@ The second element is: 5 ## Codebyte Example -In this example, the code retrieves a constant iterator for a specific bucket in the unordered_set and prints all elements stored in that bucket: +In this example, the code retrieves a constant iterator for a specific bucket in the `unordered_set` and prints all elements stored in that bucket: ```codebyte/cpp #include From 5d3c47b61b66df020f51b770dd8d8f0970e95dc6 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Wed, 3 Dec 2025 17:03:29 +0530 Subject: [PATCH 7/7] Format fix --- content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md b/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md index afb6e078b78..58d604ce588 100644 --- a/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md +++ b/content/cpp/concepts/unordered-set/terms/cbegin/cbegin.md @@ -19,7 +19,7 @@ The **`cbegin()`** method returns a constant iterator that points to the first e ```pseudo unordered_set_name.cbegin(n); -```` +``` **Return value:** @@ -33,7 +33,7 @@ unordered_set_name.cbegin(n); **Parameters:** -* `n` (size_type): The bucket index. Must be less than `bucket_count()`. +- `n` (size_type): The bucket index. Must be less than `bucket_count()`. **Return value:**