In C++, std::array::crbegin() returns a read-only reverse iterator. It starts on the last element and moves toward the first one when you use ++.
The letters mean c for const and r for reverse. Use it with crend() when you want to read an array backwards. Include <array> to use it.
This is a member of std::array<T, N>, not of a plain C array and not of std::vector. A std::array has a fixed size N that is part of its type, and it lives on the stack like a normal variable.
Syntax
Call crbegin with empty brackets. It takes no arguments.
C++
auto it = nums.crbegin();
int last = *it;
Call
What it does
nums.crbegin()
Returns a read-only reverse iterator that points to the last element.
*nums.crbegin()
Reads the last element. It gives the same value as back().
With a reverse iterator, ++it moves toward the front of the array.
Library declarations
C++
const_reverse_iterator crbegin() const noexcept;
Parameters
Parameter
Description
None
crbegin() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
Const reverse iterator
A const_reverse_iterator that points to the last element. For std::array<T, 0> it equals crend().
C++ Examples
Example 1 — Print in Reverse
Read the array from last to first.
C++
#include <array>
#include <iostream>
int main() {
std::array<int, 3> nums = {10, 20, 30};
for (auto it = nums.crbegin(); it != nums.crend(); ++it) {
std::cout << *it << ' ';
}
std::cout << '\n';
return 0;
}
Output
30 20 10
How It Works
crbegin() starts on the last element, 30.
Each ++it steps one place toward the front.
The loop ends at crend(), after 10 has been printed.
Example 2 — Reverse Inside a Function
A function with a const array parameter can still walk backwards.
C++
#include <array>
#include <iostream>
void printBackwards(const std::array<char, 4>& letters) {
for (auto it = letters.crbegin(); it != letters.crend(); ++it) {
std::cout << *it;
}
std::cout << '\n';
}
int main() {
std::array<char, 4> word = {'C', 'O', 'D', 'E'};
printBackwards(word);
return 0;
}
Output
EDOC
How It Works
The parameter is const, so only read-only iterators are possible.
The loop begins with E, the last letter, and ends with C.
The letters are printed with no spaces, so CODE becomes EDOC.
Example 3 — Check a Palindrome
Compare the array from the front with the same array from the back.