Home C++ Arrays Functions rend() C++ std::array rend() Function Definition In C++, std::array::rend() returns a reverse iterator that marks the end of a backward walk. It points to the place just before the first element.
Use it as the stop mark in loops that start with rbegin(). 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 rend with empty brackets. It takes no arguments.
for (auto it = nums.rbegin(); it != nums.rend(); ++it) {
// use *it
} Call What it does nums.rend()Returns the stop mark for a backward loop. nums.rend() - nums.rbegin()Counts the steps in a backward walk, which equals size().
Do not read *nums.rend(). It is not an element.
Library declarations reverse_iterator rend() noexcept;
const_reverse_iterator rend() const noexcept; Parameters Parameter Description None rend() takes no parameters. Call it with empty brackets.
Return Value Return Value Description Reverse iterator A reverse_iterator that marks the end of the reverse range. On a const array it is a const_reverse_iterator.
C++ Examples 1. Reverse letters 2. Count steps 3. Last match Example 1 — Loop Until rend() Print an array of letters from the last one to the first one.
#include <array>
#include <iostream>
int main() {
std::array<char, 3> letters = {'A', 'B', 'C'};
for (auto it = letters.rbegin(); it != letters.rend(); ++it) {
std::cout << *it << ' ';
}
std::cout << '\n';
return 0;
} How It Works The array holds A, B, and C in that order. The loop starts with rbegin(), which is the letter C. It stops when the iterator equals rend(), so A is the last letter printed. Example 2 — Count Backward Steps Subtract the two reverse iterators to count the elements.
#include <array>
#include <iostream>
int main() {
std::array<int, 4> nums = {9, 8, 7, 6};
auto steps = nums.rend() - nums.rbegin();
std::cout << "Reverse steps: " << steps << '\n';
return 0;
} How It Works rbegin() is the starting mark and rend() is the finishing mark.Each element takes one ++ step to cross. The array has 4 elements, so the answer is 4. Example 3 — Find the Last Match Search from the end so you get the last position, not the first.
#include <algorithm>
#include <array>
#include <iostream>
int main() {
std::array<int, 4> nums = {3, 1, 3, 2};
auto it = std::find(nums.rbegin(), nums.rend(), 3);
if (it != nums.rend()) {
std::size_t back = std::distance(nums.rbegin(), it);
std::cout << "Last 3 is at index " << nums.size() - 1 - back << '\n';
}
return 0;
} How It Works The reverse order is 2, 3, 1, 3, so the first 3 met is the one at index 2. it != nums.rend() confirms that a match really exists before it is used.std::distance gives 1 step from the back, and 4 - 1 - 1 turns that into the normal index 2.Frequently Asked Questions What does std::array::rend() return? It returns a reverse iterator that marks the end of a backward walk. It points to the place before the first element.
Can I dereference rend()? No. It is only a stop mark, so *nums.rend() is undefined behavior.
How do I turn a reverse position into a normal index? Take the distance from rbegin(), then use size() - 1 - distance. For example, distance 1 in an array of 4 means index 2.
What is the difference between rend() and begin()? begin() points to the first element. rend() points before it, and it belongs to reverse iterators, so it is used with rbegin().
Can rend() be used with an empty std::array<T, 0>? Yes. It equals rbegin(), so a reverse loop does nothing.
Next: size() Get the number of elements in the array with std::array.
size() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful
Helpful Share Copy link Suggestion