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());
Call
What 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
Parameter
Description
None
crend() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
Const reverse iterator
A const_reverse_iterator to the place before the first element. It is only for comparing, never for reading.
Empty vector
For 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;
}
Output
blue green red
How It Works
it starts on "blue", the last element.
The loop keeps going while it != colors.crend(). Each ++it moves one step towards the front.
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.
A new vector has no elements, so crbegin() and crend() are equal.
A reverse loop between them would run zero times, which makes it safe on empty data.
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.