C++ std::deque crend() Function

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

Definition

In C++, std::deque::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 deque from back to front for reading. Include <deque> to use it.

This is a member of std::deque<T>, a double-ended queue. A deque is fast at adding and removing at both ends, and it allows access by index like std::vector. Unlike a vector, its elements are stored in several blocks, not in one contiguous block.

Syntax

Call crend with empty brackets. It takes no arguments.

C++
auto stop = d.crend();
bool done = (it == d.crend());
CallWhat it does
d.crend()Returns a read-only reverse iterator that marks the end of a reverse walk.
it == d.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 dequeFor an empty deque, 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 <deque>
#include <iostream>
#include <string>

int main() {
    std::deque<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 <deque>
#include <iostream>
#include <iterator>

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

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

    std::cout << "steps: " << steps << '\n';
    std::cout << "size:  " << d.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 Deque

On an empty deque the two reverse markers are equal.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d;

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

    d.push_back(1);

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

How It Works

  1. A new deque 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: emplace()

Build a new element in place at any position (C++11) with std::deque.

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

7 people found this page helpful