C++ std::deque rbegin() Function

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

Definition

In C++, std::deque::rbegin() returns a reverse iterator that points to the last element of the deque. Moving it with ++ goes backwards, towards the first element.

Use it with rend() to walk through the deque from back to front. It can also change elements. 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 rbegin with empty brackets. It takes no arguments.

C++
auto it = d.rbegin();
*it = 99;   // changes the last element
CallWhat it does
d.rbegin()Returns a reverse iterator that starts on the last element.
*d.rbegin() = 99Changes the last element through the reverse iterator.

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

Library declarations
C++
reverse_iterator       rbegin() noexcept;
const_reverse_iterator rbegin() const noexcept;

Parameters

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

Return Value

Return ValueDescription
Reverse iteratorA reverse_iterator to the last element. On a const deque it is a const_reverse_iterator.
Empty dequeFor an empty deque, rbegin() equals rend().

C++ Examples

Example 1 — Loop Backwards with rbegin()

A reverse loop starts at rbegin() and ends at rend().

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

int main() {
    std::deque<int> d = {10, 20, 30};

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

How It Works

  1. rbegin() starts on 30, the last element.
  2. Each ++it moves one place towards the front, so the order is 30 20 10.
  3. The loop stops at rend(). The deque is not reordered.

Example 2 — Change Values Through rbegin()

A reverse iterator that is not const can write to the elements.

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

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

    auto it = d.rbegin();
    *it = 99;
    ++it;
    *it += 1;

    for (int n : d) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. *it = 99 replaces the last element, which was 3.
  2. ++it moves the reverse iterator one place towards the front, onto 2.
  3. *it += 1 changes that element to 3. The deque is now 1 3 99.

Example 3 — Make a Reversed Copy

Pass the reverse iterator pair to a constructor to build a reversed deque.

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

int main() {
    std::deque<int> d = {1, 2, 3, 4};
    std::deque<int> rev(d.rbegin(), d.rend());

    for (int n : d) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    for (int n : rev) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. rev(d.rbegin(), d.rend()) copies the elements in the order that the reverse iterators visit them.
  2. That order is back to front, so rev becomes 4 3 2 1.
  3. The original deque d is not changed. It is still 1 2 3 4.

Frequently Asked Questions

It returns a reverse iterator that points to the last element of the deque. Incrementing it moves towards the first element.
end() points after the last element and cannot be read. rbegin() points to the last element and can be read. They also move in opposite directions.
No. The deque stays the same. Only the direction of the walk changes.
Yes, on a non-const deque. Use crbegin() if you want a read-only reverse iterator.
rend(). It marks the place just before the first element, so it is the end marker for reverse walking.

Next: rend()

Get the reverse end marker, just before the first element with std::deque.

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

14 people found this page helpful