Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


The C++ deque::rend function is used to return the reverse iterator pointing to the element preceding the first element (reversed past-the-last element) of the deque. A reverse iterator iterates in backward direction and increasing it results into moving to the beginning of the deque container. Similarly, decreasing a reverse iterator results into moving to the end of the deque container.

C++ rbegin rend

Syntax

reverse_iterator rend();
const_reverse_iterator rend() const;
reverse_iterator rend() noexcept;
const_reverse_iterator rend() const noexcept;

Parameters

No parameter is required.

Return Value

A reverse iterator to the reversed past-the-last element of the sequence container. If the sequence object is constant qualified, the function returns a const_reverse_iterator, else returns an reverse_iterator.

Time Complexity

Constant i.e, Θ(1).

Example:

In the below example, the deque::rend function is used to return the reverse iterator pointing to the element preceding the first element of the deque MyDeque.

#include <iostream>
#include <deque>
using namespace std;
 
int main (){
  deque<string> MyDeque{"Alpha","Coding","Skills"};
  deque<string>::reverse_iterator rit;

  rit = MyDeque.rend();
  rit--;
  cout<<*rit<<" ";
  rit--;
  cout<<*rit<<" ";
  rit--;
  cout<<*rit<<" ";
  return 0;
}

The output of the above code will be:

Alpha Coding Skills

Example:

Lets see another example where the deque called MyDeque contains integer values and deque::rend function is used with deque::rbegin function to specify a range including all elements of the deque container.

#include <iostream>
#include <deque>
using namespace std;
 
int main (){
  deque<int> MyDeque{10, 20, 30, 40, 50};
  deque<int>::reverse_iterator rit;

  for(rit = MyDeque.rbegin(); rit != MyDeque.rend(); ++rit)
    cout<<*rit<<" ";

  return 0;
}

The output of the above code will be:

50 40 30 20 10  

❮ C++ - Deque