From a49718762093209d0752eb3e055b1bcc3c3d5abe Mon Sep 17 00:00:00 2001 From: Kenxpx Date: Mon, 19 May 2025 22:30:25 +0530 Subject: [PATCH 01/11] feat: Add term entry for C++ Arrays begin() --- .../cpp/concepts/arrays/terms/begin/begin.md | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 content/cpp/concepts/arrays/terms/begin/begin.md diff --git a/content/cpp/concepts/arrays/terms/begin/begin.md b/content/cpp/concepts/arrays/terms/begin/begin.md new file mode 100644 index 00000000000..7171664c97a --- /dev/null +++ b/content/cpp/concepts/arrays/terms/begin/begin.md @@ -0,0 +1,113 @@ +--- +Title: 'C++ Arrays begin()' +Description: 'Explains how to use begin() to get an iterator pointing to the first element in C++ std::array containers and C-style arrays via std::begin().' +Subjects: + - 'C++' + - 'Computer Science' + - 'Code Foundations' +Tags: + - 'arrays' + - 'iterators' + - 'pointers' + - 'memory access' +CatalogContent: + - 'learn-c-plus-plus' # Example: Replace with actual relevant course/path slug if known +--- + +The `begin()` function or method is used in C++ to obtain an **iterator** pointing to the first element of a container, such as an `std::array`, or a C-style array when used with `std::begin()`. This iterator can then be used to access or traverse the elements of the array. + +## Syntax + +The way `begin()` is used depends on the type of array: + +For `std::array` (a container class introduced in C++11): +```pseudo +std_array_object.begin() +``` +- **Return Value:** Returns an iterator of the appropriate type, pointing to the first element of the `std_array_object`. If the array is empty, the returned iterator will be equal to the iterator returned by `end()`. + +For C-style arrays (using `std::begin` from the `` header): +```pseudo +std::begin(c_style_array_name) +``` +- **Parameters:** + - `c_style_array_name`: The name of the C-style array. +- **Return Value:** Returns a pointer (which acts as an iterator for C-style arrays) to the first element of `c_style_array_name`. For an empty C-style array (0 elements, though less common), its behavior is specific to how such an array is defined. + +## Example + +Here's how you can use `begin()` with an `std::array`: + +```cpp +#include +#include // Required for std::array +#include // Required for std::begin (for C-style arrays example) + +int main() { + // Example with std::array + std::array numbers = {10, 20, 30, 40, 50}; + + // Get an iterator to the first element + std::array::iterator it = numbers.begin(); + // Or using auto for type deduction: + // auto it = numbers.begin(); + + // Access the first element + if (it != numbers.end()) { // Good practice: check if the array is not empty + std::cout << "First element of std::array: " << *it << std::endl; + } + + // Example with a C-style array using std::begin() + int c_style_numbers[] = {5, 15, 25}; + int* ptr_begin = std::begin(c_style_numbers); + + // Access the first element + // (Assuming c_style_numbers is not empty) + std::cout << "First element of C-style array: " << *ptr_begin << std::endl; +} +``` +This example would output: +```shell +First element of std::array: 10 +First element of C-style array: 5 +``` + +## Codebyte Example + +The following example demonstrates the use of `std::array::begin()` to access and print the first element of an `std::array`. + +```codebyte/cpp +#include +#include // Required for std::array + +// It's common to include CMakeLists.txt content if complex compilation is needed. +// For simple, single-file Codebytes like this, it's often handled by the environment. +// A typical CMakeLists.txt for a simple executable would be: +// cmake_minimum_required(VERSION 3.10) +// project(BeginExample) +// set(CMAKE_CXX_STANDARD 17) +// add_executable(BeginExample main.cpp) + +int main() { + std::array myArray = {100, 200, 300}; + + // Get an iterator to the beginning of the array + auto it = myArray.begin(); + + // Check if the array is not empty and print the first element + if (it != myArray.end()) { + std::cout << "The first element is: " << *it << std::endl; + } else { + std::cout << "The array is empty." << std::endl; + } + + // You can also use the iterator to traverse the array + std::cout << "Array elements: "; + for (auto current_it = myArray.begin(); current_it != myArray.end(); ++current_it) { + std::cout << *current_it << " "; + } + std::cout << std::endl; + + return 0; +} +``` From b2704213dcf2ff1ae7629ddd5b98f72fc5cbacd1 Mon Sep 17 00:00:00 2001 From: Ken <155082290+Kenxpx@users.noreply.github.com> Date: Wed, 21 May 2025 23:28:54 +0530 Subject: [PATCH 02/11] Update content/cpp/concepts/arrays/terms/begin/begin.md Co-authored-by: Mamta Wardhani --- .../cpp/concepts/arrays/terms/begin/begin.md | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/content/cpp/concepts/arrays/terms/begin/begin.md b/content/cpp/concepts/arrays/terms/begin/begin.md index 7171664c97a..be48ee43dca 100644 --- a/content/cpp/concepts/arrays/terms/begin/begin.md +++ b/content/cpp/concepts/arrays/terms/begin/begin.md @@ -1,17 +1,16 @@ --- -Title: 'C++ Arrays begin()' -Description: 'Explains how to use begin() to get an iterator pointing to the first element in C++ std::array containers and C-style arrays via std::begin().' +Title: '.begin()' +Description: 'Returns an iterator pointing to the first element of a C++ array or container.' Subjects: - - 'C++' - - 'Computer Science' - 'Code Foundations' + - 'Computer Science' Tags: - - 'arrays' - - 'iterators' - - 'pointers' - - 'memory access' + - 'Arrays' + - 'Iterators' + - 'Pointers' CatalogContent: - - 'learn-c-plus-plus' # Example: Replace with actual relevant course/path slug if known + - 'learn-c-plus-plus' + - 'paths/computer-science' --- The `begin()` function or method is used in C++ to obtain an **iterator** pointing to the first element of a container, such as an `std::array`, or a C-style array when used with `std::begin()`. This iterator can then be used to access or traverse the elements of the array. From 3f680dab4dbadab88ea5ba9afce67cc9c7918a84 Mon Sep 17 00:00:00 2001 From: Ken <155082290+Kenxpx@users.noreply.github.com> Date: Wed, 21 May 2025 23:29:03 +0530 Subject: [PATCH 03/11] Update content/cpp/concepts/arrays/terms/begin/begin.md Co-authored-by: Mamta Wardhani --- .../cpp/concepts/arrays/terms/begin/begin.md | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/content/cpp/concepts/arrays/terms/begin/begin.md b/content/cpp/concepts/arrays/terms/begin/begin.md index be48ee43dca..33620a46181 100644 --- a/content/cpp/concepts/arrays/terms/begin/begin.md +++ b/content/cpp/concepts/arrays/terms/begin/begin.md @@ -43,26 +43,26 @@ Here's how you can use `begin()` with an `std::array`: #include // Required for std::begin (for C-style arrays example) int main() { - // Example with std::array - std::array numbers = {10, 20, 30, 40, 50}; - - // Get an iterator to the first element - std::array::iterator it = numbers.begin(); - // Or using auto for type deduction: - // auto it = numbers.begin(); - - // Access the first element - if (it != numbers.end()) { // Good practice: check if the array is not empty - std::cout << "First element of std::array: " << *it << std::endl; - } - - // Example with a C-style array using std::begin() - int c_style_numbers[] = {5, 15, 25}; - int* ptr_begin = std::begin(c_style_numbers); - - // Access the first element - // (Assuming c_style_numbers is not empty) - std::cout << "First element of C-style array: " << *ptr_begin << std::endl; + // Example with std::array + std::array numbers = {10, 20, 30, 40, 50}; + + // Get an iterator to the first element + std::array::iterator it = numbers.begin(); + // Or using auto for type deduction: + // auto it = numbers.begin(); + + // Access the first element + if (it != numbers.end()) { // Good practice: check if the array is not empty + std::cout << "First element of std::array: " << *it << std::endl; + } + + // Example with a C-style array using std::begin() + int c_style_numbers[] = {5, 15, 25}; + int* ptr_begin = std::begin(c_style_numbers); + + // Access the first element + // (Assuming c_style_numbers is not empty) + std::cout << "First element of C-style array: " << *ptr_begin << std::endl; } ``` This example would output: From 749d31d29d3862a0eb06baa9b38eb62b1e33feed Mon Sep 17 00:00:00 2001 From: Ken <155082290+Kenxpx@users.noreply.github.com> Date: Wed, 21 May 2025 23:29:11 +0530 Subject: [PATCH 04/11] Update content/cpp/concepts/arrays/terms/begin/begin.md Co-authored-by: Mamta Wardhani --- content/cpp/concepts/arrays/terms/begin/begin.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/cpp/concepts/arrays/terms/begin/begin.md b/content/cpp/concepts/arrays/terms/begin/begin.md index 33620a46181..c3faed87061 100644 --- a/content/cpp/concepts/arrays/terms/begin/begin.md +++ b/content/cpp/concepts/arrays/terms/begin/begin.md @@ -13,7 +13,7 @@ CatalogContent: - 'paths/computer-science' --- -The `begin()` function or method is used in C++ to obtain an **iterator** pointing to the first element of a container, such as an `std::array`, or a C-style array when used with `std::begin()`. This iterator can then be used to access or traverse the elements of the array. +The **`.begin()`** method in C++ returns an iterator pointing to the first element of a container, such as `std::array`, or of a C-style array when used with the free function `std::begin()`. This iterator can be used to access or iterate through the array elements. ## Syntax From ab245a9fbc65a38c6064b206491e06bc29e48397 Mon Sep 17 00:00:00 2001 From: Ken <155082290+Kenxpx@users.noreply.github.com> Date: Wed, 21 May 2025 23:29:46 +0530 Subject: [PATCH 05/11] Update content/cpp/concepts/arrays/terms/begin/begin.md Co-authored-by: Mamta Wardhani --- content/cpp/concepts/arrays/terms/begin/begin.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/content/cpp/concepts/arrays/terms/begin/begin.md b/content/cpp/concepts/arrays/terms/begin/begin.md index c3faed87061..0bb923a1f86 100644 --- a/content/cpp/concepts/arrays/terms/begin/begin.md +++ b/content/cpp/concepts/arrays/terms/begin/begin.md @@ -65,7 +65,9 @@ int main() { std::cout << "First element of C-style array: " << *ptr_begin << std::endl; } ``` + This example would output: + ```shell First element of std::array: 10 First element of C-style array: 5 From ce43bd782c7496bf81427c301a50cc42c1efa628 Mon Sep 17 00:00:00 2001 From: Ken <155082290+Kenxpx@users.noreply.github.com> Date: Wed, 21 May 2025 23:29:53 +0530 Subject: [PATCH 06/11] Update content/cpp/concepts/arrays/terms/begin/begin.md Co-authored-by: Mamta Wardhani --- .../cpp/concepts/arrays/terms/begin/begin.md | 48 ++++++++----------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/content/cpp/concepts/arrays/terms/begin/begin.md b/content/cpp/concepts/arrays/terms/begin/begin.md index 0bb923a1f86..b33b96bf71d 100644 --- a/content/cpp/concepts/arrays/terms/begin/begin.md +++ b/content/cpp/concepts/arrays/terms/begin/begin.md @@ -81,34 +81,26 @@ The following example demonstrates the use of `std::array::begin()` to access an #include #include // Required for std::array -// It's common to include CMakeLists.txt content if complex compilation is needed. -// For simple, single-file Codebytes like this, it's often handled by the environment. -// A typical CMakeLists.txt for a simple executable would be: -// cmake_minimum_required(VERSION 3.10) -// project(BeginExample) -// set(CMAKE_CXX_STANDARD 17) -// add_executable(BeginExample main.cpp) - int main() { - std::array myArray = {100, 200, 300}; - - // Get an iterator to the beginning of the array - auto it = myArray.begin(); - - // Check if the array is not empty and print the first element - if (it != myArray.end()) { - std::cout << "The first element is: " << *it << std::endl; - } else { - std::cout << "The array is empty." << std::endl; - } - - // You can also use the iterator to traverse the array - std::cout << "Array elements: "; - for (auto current_it = myArray.begin(); current_it != myArray.end(); ++current_it) { - std::cout << *current_it << " "; - } - std::cout << std::endl; - - return 0; + std::array myArray = {100, 200, 300}; + + // Get an iterator to the beginning of the array + auto it = myArray.begin(); + + // Check if the array is not empty and print the first element + if (it != myArray.end()) { + std::cout << "The first element is: " << *it << std::endl; + } else { + std::cout << "The array is empty." << std::endl; + } + + // You can also use the iterator to traverse the array + std::cout << "Array elements: "; + for (auto current_it = myArray.begin(); current_it != myArray.end(); ++current_it) { + std::cout << *current_it << " "; + } + std::cout << std::endl; + + return 0; } ``` From adce3dc47f8283da133315c9863fc06f66713054 Mon Sep 17 00:00:00 2001 From: Ken <155082290+Kenxpx@users.noreply.github.com> Date: Wed, 21 May 2025 23:30:03 +0530 Subject: [PATCH 07/11] Update content/cpp/concepts/arrays/terms/begin/begin.md Co-authored-by: Mamta Wardhani --- content/cpp/concepts/arrays/terms/begin/begin.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/cpp/concepts/arrays/terms/begin/begin.md b/content/cpp/concepts/arrays/terms/begin/begin.md index b33b96bf71d..4000a68a279 100644 --- a/content/cpp/concepts/arrays/terms/begin/begin.md +++ b/content/cpp/concepts/arrays/terms/begin/begin.md @@ -17,7 +17,7 @@ The **`.begin()`** method in C++ returns an iterator pointing to the first eleme ## Syntax -The way `begin()` is used depends on the type of array: +The usage of begin() depends on the type of array: For `std::array` (a container class introduced in C++11): ```pseudo From b39a12dfce5f8fb610b02aa5ec35ce495b9b372a Mon Sep 17 00:00:00 2001 From: Ken <155082290+Kenxpx@users.noreply.github.com> Date: Wed, 21 May 2025 23:30:11 +0530 Subject: [PATCH 08/11] Update content/cpp/concepts/arrays/terms/begin/begin.md Co-authored-by: Mamta Wardhani --- content/cpp/concepts/arrays/terms/begin/begin.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/content/cpp/concepts/arrays/terms/begin/begin.md b/content/cpp/concepts/arrays/terms/begin/begin.md index 4000a68a279..657c38d8351 100644 --- a/content/cpp/concepts/arrays/terms/begin/begin.md +++ b/content/cpp/concepts/arrays/terms/begin/begin.md @@ -19,7 +19,8 @@ The **`.begin()`** method in C++ returns an iterator pointing to the first eleme The usage of begin() depends on the type of array: -For `std::array` (a container class introduced in C++11): +1\. For `std::array` (from ``, introduced in C++11): + ```pseudo std_array_object.begin() ``` From 573a50b269ca6f36cf0f299c5790c0c0d1c85a0c Mon Sep 17 00:00:00 2001 From: Ken <155082290+Kenxpx@users.noreply.github.com> Date: Wed, 21 May 2025 23:30:26 +0530 Subject: [PATCH 09/11] Update content/cpp/concepts/arrays/terms/begin/begin.md Co-authored-by: Mamta Wardhani --- content/cpp/concepts/arrays/terms/begin/begin.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/content/cpp/concepts/arrays/terms/begin/begin.md b/content/cpp/concepts/arrays/terms/begin/begin.md index 657c38d8351..70d8a9cc4cc 100644 --- a/content/cpp/concepts/arrays/terms/begin/begin.md +++ b/content/cpp/concepts/arrays/terms/begin/begin.md @@ -26,7 +26,8 @@ std_array_object.begin() ``` - **Return Value:** Returns an iterator of the appropriate type, pointing to the first element of the `std_array_object`. If the array is empty, the returned iterator will be equal to the iterator returned by `end()`. -For C-style arrays (using `std::begin` from the `` header): +2\. For C-style arrays (using `std::begin` from the `` header): + ```pseudo std::begin(c_style_array_name) ``` From 4af8b60a69196d237c099c16f144f48ad9fbe2e7 Mon Sep 17 00:00:00 2001 From: Ken <155082290+Kenxpx@users.noreply.github.com> Date: Wed, 21 May 2025 23:30:37 +0530 Subject: [PATCH 10/11] Update content/cpp/concepts/arrays/terms/begin/begin.md Co-authored-by: Mamta Wardhani --- content/cpp/concepts/arrays/terms/begin/begin.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/content/cpp/concepts/arrays/terms/begin/begin.md b/content/cpp/concepts/arrays/terms/begin/begin.md index 70d8a9cc4cc..f73a8063e05 100644 --- a/content/cpp/concepts/arrays/terms/begin/begin.md +++ b/content/cpp/concepts/arrays/terms/begin/begin.md @@ -24,7 +24,9 @@ The usage of begin() depends on the type of array: ```pseudo std_array_object.begin() ``` -- **Return Value:** Returns an iterator of the appropriate type, pointing to the first element of the `std_array_object`. If the array is empty, the returned iterator will be equal to the iterator returned by `end()`. + +**Return value:** +Returns an iterator of the appropriate type, pointing to the first element of the `std_array_object`. If the array is empty, the returned iterator will be equal to the iterator returned by `std_array_object.end()`. 2\. For C-style arrays (using `std::begin` from the `` header): From e2125c58dd7ef5597dc19420f08f5e64fb087e4b Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Wed, 28 May 2025 12:09:11 +0530 Subject: [PATCH 11/11] minor fix --- .../cpp/concepts/arrays/terms/begin/begin.md | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/content/cpp/concepts/arrays/terms/begin/begin.md b/content/cpp/concepts/arrays/terms/begin/begin.md index f73a8063e05..763cfb72fd0 100644 --- a/content/cpp/concepts/arrays/terms/begin/begin.md +++ b/content/cpp/concepts/arrays/terms/begin/begin.md @@ -17,7 +17,7 @@ The **`.begin()`** method in C++ returns an iterator pointing to the first eleme ## Syntax -The usage of begin() depends on the type of array: +The usage of `.begin()` depends on the type of array: 1\. For `std::array` (from ``, introduced in C++11): @@ -26,6 +26,7 @@ std_array_object.begin() ``` **Return value:** + Returns an iterator of the appropriate type, pointing to the first element of the `std_array_object`. If the array is empty, the returned iterator will be equal to the iterator returned by `std_array_object.end()`. 2\. For C-style arrays (using `std::begin` from the `` header): @@ -33,13 +34,18 @@ Returns an iterator of the appropriate type, pointing to the first element of th ```pseudo std::begin(c_style_array_name) ``` -- **Parameters:** - - `c_style_array_name`: The name of the C-style array. -- **Return Value:** Returns a pointer (which acts as an iterator for C-style arrays) to the first element of `c_style_array_name`. For an empty C-style array (0 elements, though less common), its behavior is specific to how such an array is defined. + +**Parameters:** + + - `c_style_array_name`: The name of the C-style array. + +**Return Value:** + +Returns a pointer (which acts as an iterator for C-style arrays) to the first element of `c_style_array_name`. For an empty C-style array (0 elements, though less common), its behavior is specific to how such an array is defined. ## Example -Here's how you can use `begin()` with an `std::array`: +Here's how `.begin()` can be used with an `std::array`: ```cpp #include @@ -83,7 +89,7 @@ The following example demonstrates the use of `std::array::begin()` to access an ```codebyte/cpp #include -#include // Required for std::array +#include int main() { std::array myArray = {100, 200, 300};