C++ std::array crend() Function

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

Definition

In C++, std::array::crend() returns a read-only 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 when you loop from crbegin(). 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 crend with empty brackets. It takes no arguments.

C++
for (auto it = nums.crbegin(); it != nums.crend(); ++it) {
    // read *it
}
CallWhat it does
nums.crend()Returns the stop mark for a read-only backward loop.
std::distance(nums.crbegin(), nums.crend())Counts how many steps a reverse walk takes.

Like cend(), this iterator is only a mark. It does not point to a real element.

Library declarations
C++
const_reverse_iterator crend() const noexcept;

Parameters

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

Return Value

Return ValueDescription
Const reverse iteratorA const_reverse_iterator that marks the end of the reverse range. For std::array<T, 0> it equals crbegin().

C++ Examples

Example 1 — Loop Until crend()

Count each step while you walk backwards.

C++
#include <array>
#include <iostream>

int main() {
    std::array<int, 3> nums = {10, 20, 30};

    int step = 1;
    for (auto it = nums.crbegin(); it != nums.crend(); ++it, ++step) {
        std::cout << "Step " << step << ": " << *it << '\n';
    }
    return 0;
}

How It Works

  1. The variable step starts at 1 and grows with the iterator.
  2. The loop keeps running while it is not crend().
  3. The last element is printed first, so step 1 shows 30.

Example 2 — Count Steps with std::distance

Find how many steps lie between the two reverse marks.

C++
#include <array>
#include <iostream>
#include <iterator>

int main() {
    std::array<int, 5> nums = {4, 8, 15, 16, 23};

    auto steps = std::distance(nums.crbegin(), nums.crend());

    std::cout << "Items in reverse: " << steps << '\n';
    return 0;
}

How It Works

  1. std::distance counts how many ++ moves take you from the first iterator to the second.
  2. A full reverse walk visits every element exactly once.
  3. The array has 5 elements, so the result is 5.

Example 3 — Find the Last Negative Number

Search from the back. Use crend() to check whether anything was found.

C++
#include <algorithm>
#include <array>
#include <iostream>

int main() {
    std::array<int, 5> nums = {4, -1, 6, -3, 8};

    auto it = std::find_if(nums.crbegin(), nums.crend(),
                           [](int n) { return n < 0; });

    if (it != nums.crend()) {
        std::cout << "Last negative: " << *it << '\n';
    } else {
        std::cout << "No negative numbers\n";
    }
    return 0;
}

How It Works

  1. The search starts at the back, so 8 and 6 are skipped first.
  2. -3 is the first negative number met on the way, so find_if stops there.
  3. If nothing matched, find_if would return crend(), which is why the code compares against it.

Frequently Asked Questions

It returns a const reverse iterator that marks the end of a backward walk. It points to the place just before the first element.
No. It is only a stop mark and does not point to a real element.
cend() marks the end of a forward walk and goes with cbegin(). crend() marks the end of a backward walk and goes with crbegin().
Algorithms such as std::find_if return crend() when nothing matches. Compare the result with nums.crend().
They mark the same place. On a non-const array, rend() belongs to iterators that can change elements, while crend() belongs to read-only ones.

Next: data()

Get a raw pointer to the array memory with std::array.

data() 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.

13 people found this page helpful