C++ std::deque crbegin() Function

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

Definition

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

The letters cr mean const reverse: you walk from back to front, and you can only read the values. 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 crbegin with empty brackets. It takes no arguments.

C++
auto it = d.crbegin();
int last = *it;
CallWhat it does
d.crbegin()Returns a read-only reverse iterator that starts on the last element.
*d.crbegin()Reads the last element. It gives the same value as d.back().

If you need to change elements while walking backwards, use rbegin() instead.

Library declarations
C++
const_reverse_iterator crbegin() const noexcept;

Parameters

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

Return Value

Return ValueDescription
Const reverse iteratorA const_reverse_iterator to the last element. ++it moves towards the front.
Empty dequeFor an empty deque, crbegin() equals crend().

C++ Examples

Example 1 — Print a Deque Backwards

A reverse loop starts at crbegin() and ends at crend().

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

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

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

How It Works

  1. crbegin() starts on 4, the last element.
  2. Each ++it moves one step towards the front, so the order is 4 3 2 1.
  3. The deque itself is not changed. Only the walking direction is reversed.

Example 2 — *crbegin() Equals back()

The first reverse element is the same as the last normal element.

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

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

    std::cout << "*crbegin(): " << *d.crbegin() << '\n';
    std::cout << "back():     " << d.back() << '\n';
    return 0;
}

How It Works

  1. A reverse iterator starts at the back of the deque, so crbegin() points to 9.
  2. back() returns the same element directly as a reference.
  3. Use back() when you want one value. Use crbegin() when you want to walk or pass a range to an algorithm.

Example 3 — Search from the Back with crbegin()

Find the last match of a value instead of the first one.

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

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

    auto it = std::find(d.crbegin(), d.crend(), 5);
    std::size_t fromBack = std::distance(d.crbegin(), it);

    std::cout << "last 5 is " << fromBack << " place(s) from the back\n";
    std::cout << "forward index: " << (d.size() - 1 - fromBack) << '\n';
    return 0;
}

How It Works

  1. Searching with reverse iterators checks 9 first, then 5, so it finds the match that is closest to the back.
  2. std::distance(d.crbegin(), it) counts the steps taken from the back. Here it is 1.
  3. size() - 1 - fromBack turns that into a normal index: 4 - 1 - 1 = 2. The last 5 is at index 2.

Frequently Asked Questions

It returns a const_reverse_iterator that points to the last element of the deque. Incrementing it moves towards the first element.
rbegin() can return a changeable reverse iterator. crbegin() always returns a const_reverse_iterator, so you can read the elements but not modify them.
No. The deque is not changed. Only the direction you walk through it is reversed.
They point to the same element, but they are different types. crbegin() moves backwards with ++, while cend() - 1 is a normal iterator that moves forwards with ++.
crend(). It marks the place just before the first element, so it is the end marker for reverse walking.

Next: crend()

Get a read-only reverse end marker, just before the first element with std::deque.

crend() 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