C++ std::array rend() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::array

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.

C++
for (auto it = nums.rbegin(); it != nums.rend(); ++it) {
    // use *it
}
CallWhat 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
C++
reverse_iterator       rend() noexcept;
const_reverse_iterator rend() const noexcept;

Parameters

ParameterDescription
Nonerend() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
Reverse iteratorA reverse_iterator that marks the end of the reverse range. On a const array it is a const_reverse_iterator.

C++ Examples

Example 1 — Loop Until rend()

Print an array of letters from the last one to the first one.

C++
#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

  1. The array holds A, B, and C in that order.
  2. The loop starts with rbegin(), which is the letter C.
  3. 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.

C++
#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

  1. rbegin() is the starting mark and rend() is the finishing mark.
  2. Each element takes one ++ step to cross.
  3. 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.

C++
#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

  1. The reverse order is 2, 3, 1, 3, so the first 3 met is the one at index 2.
  2. it != nums.rend() confirms that a match really exists before it is used.
  3. std::distance gives 1 step from the back, and 4 - 1 - 1 turns that into the normal index 2.

Frequently Asked Questions

It returns a reverse iterator that marks the end of a backward walk. It points to the place before the first element.
No. It is only a stop mark, so *nums.rend() is undefined behavior.
Take the distance from rbegin(), then use size() - 1 - distance. For example, distance 1 in an array of 4 means index 2.
begin() points to the first element. rend() points before it, and it belongs to reverse iterators, so it is used with rbegin().
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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