deque_rbegin( ) in C++ in STL


Given is the task to show the functionality of Deque rbegin( ) function in C++ STL

What is Deque?

Deque is the Double Ended Queues that are the sequence containers which provides the functionality of expansion and contraction on both the ends. A queue data structure allow user to insert data only at the END and delete data from the FRONT. Let’s take the analogy of queues at bus stops where the person can be inserted to a queue from the END only and the person standing in the FRONT is the first to be removed whereas in Double ended queue the insertion and deletion of data is possible at both the ends.

What is rbegin( ) function?

The rbegin( ) function returns a reverse iterator pointing to the last element in the deque, the regin( ) function reverse the deque.

Syntax − deque_name.rbegin( )

Return Value − It returns a reverse iterator which points to the last element of the deque.

Example

Input Deque − 10 9 8 7 6 5 4 3 2 1

Output Reversed Deque − 1 2 3 4 5 6 7 8 9 10

Input  Deque − G O L D E N

Output Reversed Deque − N E D L O G

Approach can be followed

  • First we declare the deque.

  • Then we print the deque.

  • Then we use the rbegin( ) function.

  • Then we print the new deque after reversing operation.

By using above approach we can get the reversed deque

Example

// C++ code to demonstrate the working of deque rbegin( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main ( ){
   // initializing the deque
   Deque<int> deque = { 5, 4, 0, 8, 5 };
   // print the deque
   cout<< “ Deque: “;
   for( auto x = deque.begin( ); x != deque.end( ); ++x)
      cout<< *x << “ “;
   // printing reverse deque
   cout<< “ Reversed deque: ”;
   for( x = deque.rbegin( ) ; x != deque.rend( ); ++x)
      cout<< “ “ <<*x;
   return 0;
}

Output

If we run the above code then it will generate the following output

Input - Deque: 5 4 0 8 5
Output - Reversed Deque: 5 8 0 4 5

Example

// C++ code to demonstrate the working of deque rbegin( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main( ){
   // initializing deque
   deque<char> deque ={ ‘P’ , ‘R’ , ‘O’ , ‘D’ , ‘U’ , ‘C’ , ‘T’ };
   cout<< “ Deque: “;
   for( auto x = deque.begin( ); x != deque.end( ); ++x)
      cout<< *x << “ “;
   // printing reversed deque
   cout<< “ Reversed deque:”;
   for( x = deque.rbegin( ) ; x != deque.rend( ); ++x)
      cout<< “ “ <<*x;
   return 0;
}

Output

If we run the above code then it will generate the following output

Input – Deque: P R O D U C T
Output – Reversed deque : T C U D O R P

Updated on: 26-Feb-2020

93 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements