C++ std::deque rend() Function

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

Definition

In C++, std::deque::rend() returns a 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 rbegin(). Together they describe the deque from back to front. 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 rend with empty brackets. It takes no arguments.

C++
auto stop = d.rend();
auto first = d.rend() - 1;   // only if d is not empty
CallWhat it does
d.rend()Returns a reverse iterator that marks the end of a reverse walk.
d.rend() - 1Steps back onto the first element. This is safe only when the deque is not empty.

For a read-only version, use crend().

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 to the place before the first element. It is only for comparing, never for reading.
Empty dequeFor an empty deque, rend() equals rbegin().

C++ Examples

Example 1 — Stop a Reverse Loop with rend()

The loop runs while the reverse iterator is not equal to rend().

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

int main() {
    std::deque<int> d = {2, 3, 4};

    for (auto it = d.rbegin(); it != d.rend(); ++it) {
        std::cout << *it << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. rbegin() starts on 4, the last element.
  2. After 2, one more ++it makes the iterator equal to rend().
  3. The condition it != d.rend() is now false, so the loop stops before reading anything invalid.

Example 2 — Reach the First Element with rend() - 1

Step back from the end marker onto the first element.

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

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

    auto it = d.rend() - 1;

    std::cout << "*(rend() - 1): " << *it << '\n';
    std::cout << "front():       " << d.front() << '\n';
    return 0;
}

How It Works

  1. d.rend() is the marker before the first element, so it cannot be read.
  2. - 1 moves the reverse iterator one step back in the reverse direction, which lands on the first element, 5.
  3. This is the same value as front(). In real code, front() is simpler.

Example 3 — Search from the Back and Compare with rend()

Find the last value above a limit. If nothing matches, the result equals rend().

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

int main() {
    std::deque<int> d = {5, 20, 3, 15, 1};

    auto a = std::find_if(d.rbegin(), d.rend(), [](int n) { return n > 10; });
    auto b = std::find_if(d.rbegin(), d.rend(), [](int n) { return n > 100; });

    if (a != d.rend()) {
        std::cout << "last value above 10: " << *a << '\n';
    }
    if (b == d.rend()) {
        std::cout << "none above 100\n";
    }
    return 0;
}

How It Works

  1. Searching with reverse iterators starts from the back, so find_if sees 1, then 15.
  2. 15 is above 10, so a points to it and a != d.rend() is true. The match closest to the back wins, even though 20 also fits.
  3. No value is above 100, so find_if walks all the way and returns rend(). That is how you detect not found.

Frequently Asked Questions

It returns a 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.rend() is undefined behavior.
begin() points to the first element and moves forward. rend() is a reverse marker before the first element and is used to stop a backwards walk.
Reverse iterators and normal iterators are different types. A reverse loop must compare against a reverse end marker, which is rend().
Yes. Both mark the same spot, so a loop between them does nothing.

Next: resize()

Change the number of elements, adding or removing at the back with std::deque.

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

15 people found this page helpful