Skip to content

[Term Entry] C++ Array Function: .cend() #7194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions content/cpp/concepts/arrays/terms/cend/cend.md
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <array>

int main() {
std::array<int,2> 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 of 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 <iostream>
#include <array>

int main() {
std::array<int,4> 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
```