From 664f39dba17c6a208b58391cb76f1320ba459e67 Mon Sep 17 00:00:00 2001 From: jayrosario0904 Date: Mon, 30 Jun 2025 20:20:43 -0700 Subject: [PATCH 1/4] Create cend term for cpp array function --- .../cpp/concepts/arrays/terms/cend/cend.md | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 content/cpp/concepts/arrays/terms/cend/cend.md diff --git a/content/cpp/concepts/arrays/terms/cend/cend.md b/content/cpp/concepts/arrays/terms/cend/cend.md new file mode 100644 index 00000000000..b1fd6b911e7 --- /dev/null +++ b/content/cpp/concepts/arrays/terms/cend/cend.md @@ -0,0 +1,70 @@ +--- +Title: '.cend()' +Description: 'Returns a constant iterator that points to the element after the last element in the array.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Arrays' + - 'Iterators' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +**`cend()`** takes no parameters and returns a constant iterator pointing to the element +after the last element in the array + +## Syntax +```pseudo +array.cend(); +``` + +## Example: Using cend to get Last Element + +This example prints the last element using cend() + +```cpp + +#include +#include + +int main() { + std::array array = {1,2}; + //gets iterator after last element + auto it = array.cend(); + //uses iterator to get last element 2 + std::cout << *(std::prev(it)) << "\n"; + return 0; +} +``` + +The output pf the program above will be: + +```shell +2 +``` + +## Codebyte Example: Using Cend to print array + +The following code makes an array and uses cend to print out all of its elements. + +```codebyte/cpp +#include +#include + +int main() { + std::array array = {1, 2, 3, 4}; + + //prints all elements in array + for(auto i = array.cbegin(); i != array.cend(); ++i){ + std::cout << *i << " "; + } + + return 0; +} +``` +The output of the program above will be +```shell +1 2 3 4 +``` \ No newline at end of file From a31b288919605e7f2f902b984bed29181f109b44 Mon Sep 17 00:00:00 2001 From: jayrosario0904 Date: Mon, 30 Jun 2025 20:32:13 -0700 Subject: [PATCH 2/4] Create cend term for cpp array function --- content/cpp/concepts/arrays/terms/cend/cend.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/cpp/concepts/arrays/terms/cend/cend.md b/content/cpp/concepts/arrays/terms/cend/cend.md index b1fd6b911e7..6095524e4df 100644 --- a/content/cpp/concepts/arrays/terms/cend/cend.md +++ b/content/cpp/concepts/arrays/terms/cend/cend.md @@ -39,7 +39,7 @@ int main() { } ``` -The output pf the program above will be: +The output of the program above will be: ```shell 2 From c00bc6658207f2d0c8c14915a9f884b02543d1a6 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 5 Jul 2025 12:18:22 +0530 Subject: [PATCH 3/4] content fixes --- .../cpp/concepts/arrays/terms/cend/cend.md | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/content/cpp/concepts/arrays/terms/cend/cend.md b/content/cpp/concepts/arrays/terms/cend/cend.md index 6095524e4df..7d9684499bc 100644 --- a/content/cpp/concepts/arrays/terms/cend/cend.md +++ b/content/cpp/concepts/arrays/terms/cend/cend.md @@ -1,6 +1,6 @@ --- Title: '.cend()' -Description: 'Returns a constant iterator that points to the element after the last element in the array.' +Description: 'Returns a constant iterator pointing just past the last element of an array container.' Subjects: - 'Code Foundations' - 'Computer Science' @@ -12,59 +12,65 @@ CatalogContent: - 'paths/computer-science' --- -**`cend()`** takes no parameters and returns a constant iterator pointing to the element -after the last element in the array +**`.cend()`** is a member function of standard C++ containers (such as [arrays](https://www.codecademy.com/resources/docs/cpp/arrays), [vectors](https://www.codecademy.com/resources/docs/cpp/vectors), and [sets](https://www.codecademy.com/resources/docs/cpp/sets)) that returns a constant iterator pointing just past the last element of the container. The "c" in `.cend()` stands for "const", indicating that the iterator cannot modify the elements it accesses. This function is commonly used for read-only traversal and is typically employed in range checks or `for` loops to define the end boundary of the container. ## Syntax + ```pseudo array.cend(); ``` -## Example: Using cend to get Last Element +**Parameters:** -This example prints the last element using cend() +This function does not take any parameters. -```cpp +**Return value:** + +Returns a constant iterator pointing just past the last element of the container. +## Example: Using `.cend()` to Access the Last Element + +This example prints the last element of the array by using `.cend()` and moving one step backward: + +```cpp #include #include int main() { - std::array array = {1,2}; - //gets iterator after last element - auto it = array.cend(); - //uses iterator to get last element 2 - std::cout << *(std::prev(it)) << "\n"; - return 0; + std::array array = {1, 2}; + + // Get constant iterator just past the last element + auto it = array.cend(); + + // Move one step back to point to the last element + std::cout << *(std::prev(it)) << "\n"; // Outputs: 2 + + return 0; } ``` -The output of the program above will be: +The output of this program will be: ```shell 2 ``` -## Codebyte Example: Using Cend to print array +## Codebyte Example: Using `.cend()` to Print an Array -The following code makes an array and uses cend to print out all of its elements. +The following code creates an array and uses `.cbegin()` and `.cend()` to print all of its elements: ```codebyte/cpp #include #include int main() { - std::array array = {1, 2, 3, 4}; + std::array array = {1, 2, 3, 4}; - //prints all elements in array - for(auto i = array.cbegin(); i != array.cend(); ++i){ + // Prints all elements in the array using const iterators + for (auto i = array.cbegin(); i != array.cend(); ++i) { std::cout << *i << " "; } return 0; } ``` -The output of the program above will be -```shell -1 2 3 4 -``` \ No newline at end of file From 95411372b6a87e52123ccb3b7bbd7dcaaedaa421 Mon Sep 17 00:00:00 2001 From: Sriparno Roy Date: Mon, 14 Jul 2025 09:37:48 +0530 Subject: [PATCH 4/4] Minor changes --- content/cpp/concepts/arrays/terms/cend/cend.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/content/cpp/concepts/arrays/terms/cend/cend.md b/content/cpp/concepts/arrays/terms/cend/cend.md index 7d9684499bc..0e8d4a8b5dc 100644 --- a/content/cpp/concepts/arrays/terms/cend/cend.md +++ b/content/cpp/concepts/arrays/terms/cend/cend.md @@ -1,18 +1,18 @@ --- Title: '.cend()' -Description: 'Returns a constant iterator pointing just past the last element of an array container.' -Subjects: +Description: 'Returns a constant iterator pointing just past the last element of an array container.' +Subjects: - 'Code Foundations' - 'Computer Science' -Tags: +Tags: - 'Arrays' - 'Iterators' -CatalogContent: +CatalogContent: - 'learn-c-plus-plus' - 'paths/computer-science' --- -**`.cend()`** is a member function of standard C++ containers (such as [arrays](https://www.codecademy.com/resources/docs/cpp/arrays), [vectors](https://www.codecademy.com/resources/docs/cpp/vectors), and [sets](https://www.codecademy.com/resources/docs/cpp/sets)) that returns a constant iterator pointing just past the last element of the container. The "c" in `.cend()` stands for "const", indicating that the iterator cannot modify the elements it accesses. This function is commonly used for read-only traversal and is typically employed in range checks or `for` loops to define the end boundary of the container. +**`.cend()`** is a member function of standard C++ containers (such as arrays, [vectors](https://www.codecademy.com/resources/docs/cpp/vectors), and [sets](https://www.codecademy.com/resources/docs/cpp/sets)) that returns a constant iterator pointing just past the last element of the container. The "c" in `.cend()` stands for "const", indicating that the iterator cannot modify the elements it accesses. This function is commonly used for read-only traversal and is typically employed in range checks or `for` loops to define the end boundary of the container. ## Syntax @@ -43,13 +43,13 @@ int main() { auto it = array.cend(); // Move one step back to point to the last element - std::cout << *(std::prev(it)) << "\n"; // Outputs: 2 + std::cout << *(std::prev(it)) << "\n"; return 0; } ``` -The output of this program will be: +The output of this code will be: ```shell 2 @@ -57,7 +57,7 @@ The output of this program will be: ## Codebyte Example: Using `.cend()` to Print an Array -The following code creates an array and uses `.cbegin()` and `.cend()` to print all of its elements: +This codebyte creates an array and uses `.cbegin()` and `.cend()` to print all of its elements: ```codebyte/cpp #include