Given the task is to show the working of deque::crbegin() in C++.

Deque is a double ended queue that gives insertion and deletion at each end i.e. front and back with high performance, in contrast to vector that gives high performance insertion at the end i.e. back only.

Also, it provides random access to components too. Though one can insert part in between alternative components in dequeue with insert(), however its performance won't be sensible rather like a vector.

What is deque::crbegin()?

Deque::crbegin(), where crbegin is the constant reverse begin, implies it constantly reverse the begin or in other words it returns the constant_reverse_iterator.

What is a constant iterator?

A constant iterator is not something which is used for modification. It's main purpose is to access them instead. For modifying the elements we use non_const iterators.



Return const_reverse_iterator to reverse from starting

Returns a const_reverse_iterator inform to the last part within the container (i.e., its reverse beginning).

Return Function

A const_reverse_iterator to the reverse starting of the sequence.

Member type const_reverse_iterator could be a reverse random access iterator type that points to a const part (see deque member types).


 Live Demo

#include <iostream>
#include <deque>
int main (){
   std::deque<int> mydeque = {1,2,3,4,5};
   std::cout << "mydeque backwards:";
   for (auto rit = mydeque.crbegin(); rit != mydeque.crend(); ++rit)
      std::cout << ' '<< *rit;
   std::cout << '\n';
   return 0;


If we run the above program it will generate the following output −

mydeque backwards: 5 4 3 2 1