C++ std::vector crend() Function

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

Definition

In C++, std::vector::crend() returns a const_reverse_iterator that marks the end of a backwards walk. It stands for the place just before the first element, so it is not a real element.

It is the partner of crbegin(): together they cover the vector from back to front for reading. Include <vector> to use it.

This is a member of std::vector<T>, a dynamic contiguous array. Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.

Syntax

Call crend with empty brackets. It takes no arguments.

C++
auto stop = v.crend();
bool done = (it == v.crend());
CallWhat it does
v.crend()Returns a read-only reverse iterator that marks the end of a reverse walk.
it == v.crend()Checks whether a reverse iterator has gone past the first element.

For a reverse end marker that allows changing elements, use rend().

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 to the place before the first element. It is only for comparing, never for reading.
Empty vectorFor an empty vector, crend() equals crbegin().

C++ Examples

Example 1 — Reverse while Loop with crend()

Move a reverse iterator by hand until it reaches the end marker.

C++
#include <vector>
#include <iostream>
#include <string>

int main() {
    std::vector<std::string> colors = {"red", "green", "blue"};

    auto it = colors.crbegin();
    while (it != colors.crend()) {
        std::cout << *it << ' ';
        ++it;
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. it starts on "blue", the last element.
  2. The loop keeps going while it != colors.crend(). Each ++it moves one step towards the front.
  3. After "red", the iterator equals crend() and the loop ends.

Example 2 — Count Elements with crend()

The distance between the two reverse markers equals the size.

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

int main() {
    std::vector<int> v = {4, 5, 6, 7, 8};

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

    std::cout << "steps: " << steps << '\n';
    std::cout << "size:  " << v.size() << '\n';
    return 0;
}

How It Works

  1. std::distance counts how many ++ steps lead from the first iterator to the second.
  2. Walking from crbegin() to crend() visits all five elements, so the answer is 5.
  3. That is the same number that size() reports.

Example 3 — crend() on an Empty Vector

On an empty vector the two reverse markers are equal.

C++
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v;

    std::cout << std::boolalpha;
    std::cout << "empty: " << (v.crbegin() == v.crend()) << '\n';

    v.push_back(1);

    std::cout << "after push_back: " << (v.crbegin() == v.crend()) << '\n';
    return 0;
}

How It Works

  1. A new vector has no elements, so crbegin() and crend() are equal.
  2. A reverse loop between them would run zero times, which makes it safe on empty data.
  3. After push_back(1) there is one element, so the two markers are no longer equal. The comparison is called again with fresh iterators.

Frequently Asked Questions

It returns a const_reverse_iterator that marks the end of a backwards walk. It stands for the place just before the first element.
No. It does not point to an element. Reading *d.crend() is undefined behavior.
Both mark the end of a reverse walk. crend() always gives a const_reverse_iterator, so you cannot change elements through it. rend() may give a changeable one.
Reverse iterators and normal iterators are different types. A reverse loop must compare against a reverse end marker, which is crend().
Yes. Both mark the same spot, so a loop between them does nothing.

Next: data()

Pointer to the contiguous element storage with std::vector.

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.

9 people found this page helpful