Skip to content

Commit 167d52e

Browse files
authored
[Term Entry] C++ Arrays: .crbegin()
* docs(cpp): added crbegin entry (1st draft for issue 6790) * changed title block a bit * changed file path * docs(cpp): complete crbegin() entry – add syntax table, example output, codebyte * chore: remove duplicate crbegin entry under arrays/terms * minor fixes * Update crbegin.md * Minor changes ---------
1 parent 5a8c650 commit 167d52e

File tree

1 file changed

+73
-0
lines changed
  • content/cpp/concepts/arrays/terms/crbegin

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
Title: '.crbegin()'
3+
Description: 'Returns a const_reverse_iterator pointing to the last element of the given array.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Developer Tools'
7+
Tags:
8+
- 'Arrays'
9+
- 'Data'
10+
- 'Data Structures'
11+
- 'Methods'
12+
CatalogContent:
13+
- 'learn-c-plus-plus'
14+
- 'path/computer-science'
15+
---
16+
17+
In C++, the **`.crbegin()`** [method](https://www.codecademy.com/resources/docs/cpp/methods) returns a `const_reverse_iterator` referring to the last element of the given array, i.e., the first element in reverse order. Because the iterator is constant, it cannot be used to modify the underlying elements.
18+
19+
## Syntax
20+
21+
```pseudo
22+
container.crbegin()
23+
```
24+
25+
**Parameters:**
26+
27+
`.crbegin()` takes no parameters.
28+
29+
**Return value:**
30+
31+
Returns a `const_reverse_iterator` referring to the last element of the given array.
32+
33+
## Example
34+
35+
This example prints the elements of an array in reverse order using constant reverse iterators (`.crbegin()` and `.crend()`):
36+
37+
```cpp
38+
#include <array>
39+
#include <iostream>
40+
41+
int main() {
42+
std::array<int, 5> nums = {10, 20, 30, 40, 50};
43+
44+
// Print the array in reverse using .crbegin() and .crend()
45+
for (auto it = nums.crbegin(); it != nums.crend(); ++it) {
46+
std::cout << *it << ' ';
47+
}
48+
std::cout << '\n';
49+
}
50+
```
51+
52+
Here is the output:
53+
54+
```shell
55+
50 40 30 20 10
56+
```
57+
58+
## Codebyte Example
59+
60+
Run this codebyte example to understand how the `.crbegin()` method works:
61+
62+
```codebyte/cpp
63+
#include <array>
64+
#include <iostream>
65+
66+
int main() {
67+
std::array<char, 4> letters = {'A', 'B', 'C', 'D'};
68+
69+
// crbegin() points to the last element (index 3)
70+
std::cout << "Last letter: " << *letters.crbegin() << '\n';
71+
return 0;
72+
}
73+
```

0 commit comments

Comments
 (0)