From e91dada090e40624f206ae03394ce08def405a4b Mon Sep 17 00:00:00 2001 From: Lokesh9106 <2400040120@kluniversity.in> Date: Mon, 24 Nov 2025 12:23:31 +0530 Subject: [PATCH 1/2] Add C++ unordered_set begin() documentation entry --- .../unordered-set/terms/begin/begin.md | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 content/cpp/concepts/unordered-set/terms/begin/begin.md diff --git a/content/cpp/concepts/unordered-set/terms/begin/begin.md b/content/cpp/concepts/unordered-set/terms/begin/begin.md new file mode 100644 index 00000000000..a2290ca6973 --- /dev/null +++ b/content/cpp/concepts/unordered-set/terms/begin/begin.md @@ -0,0 +1,72 @@ +--- +Title: '.begin()' +Description: 'Returns an iterator pointing to the first element in the unordered_set.' +Subjects: + - 'Computer Science' + - 'Code Foundations' +Tags: + - 'Methods' + - 'Unordered Sets' + - 'Iterators' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +The **`.begin()`** method returns an iterator pointing to the first element in the `unordered_set` container. Since `unordered_set` is an unordered container, the "first" element is not determined by any particular order. + +## Syntax + +```cpp +unordered_set.begin(); +``` + +The method takes no parameters and returns an iterator to the first element. If the container is empty, the returned iterator will be equal to `.end()`. + +## Example + +The following example demonstrates using `.begin()` to iterate through an `unordered_set`: + +```cpp +#include +#include + +int main() { + std::unordered_set numbers = {10, 20, 30, 40, 50}; + + std::cout << "Elements in the unordered_set: "; + for (auto it = numbers.begin(); it != numbers.end(); ++it) { + std::cout << *it << " "; + } + + return 0; +} +``` + +This outputs the elements in the unordered_set (order may vary): + +``` +Elements in the unordered_set: 50 40 30 20 10 +``` + +## Codebyte Example + +The following runnable example shows how to use `.begin()` with an `unordered_set`: + +```codebyte/cpp +#include +#include + +int main() { + std::unordered_set fruits = {"apple", "banana", "orange"}; + + std::cout << "First fruit: " << *fruits.begin() << std::endl; + + std::cout << "All fruits: "; + for (auto it = fruits.begin(); it != fruits.end(); ++it) { + std::cout << *it << " "; + } + + return 0; +} +``` From a566d757c151f4edb94bfac66c00cca7e78ef63e Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Wed, 26 Nov 2025 15:47:08 +0530 Subject: [PATCH 2/2] minor content fixes --- .../unordered-set/terms/begin/begin.md | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/content/cpp/concepts/unordered-set/terms/begin/begin.md b/content/cpp/concepts/unordered-set/terms/begin/begin.md index a2290ca6973..86c46fd53a4 100644 --- a/content/cpp/concepts/unordered-set/terms/begin/begin.md +++ b/content/cpp/concepts/unordered-set/terms/begin/begin.md @@ -2,26 +2,32 @@ Title: '.begin()' Description: 'Returns an iterator pointing to the first element in the unordered_set.' Subjects: - - 'Computer Science' - 'Code Foundations' + - 'Computer Science' Tags: - - 'Methods' - - 'Unordered Sets' - 'Iterators' + - 'Methods' + - 'Sets' CatalogContent: - 'learn-c-plus-plus' - 'paths/computer-science' --- -The **`.begin()`** method returns an iterator pointing to the first element in the `unordered_set` container. Since `unordered_set` is an unordered container, the "first" element is not determined by any particular order. +The **`.begin()`** method returns an iterator pointing to the first element in the `unordered_set` container. Because `unordered_set` does not maintain sorted order, the element returned by `.begin()` is simply the first element in its internal bucket structure, not the "smallest" or "first" in any logical sense. ## Syntax -```cpp +```pseudo unordered_set.begin(); ``` -The method takes no parameters and returns an iterator to the first element. If the container is empty, the returned iterator will be equal to `.end()`. +**Parameters:** + +The `.begin()` method takes no parameters. + +**Return value:** + +Returns an iterator to the first element. If the container is empty, the iterator equals `.end()`. ## Example @@ -45,7 +51,7 @@ int main() { This outputs the elements in the unordered_set (order may vary): -``` +```shell Elements in the unordered_set: 50 40 30 20 10 ```