deque_crend in C++ in STL


Given is the task to show the functionality of Deque crend( ) 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.

The deque crend( ) function returns the const_reverse_iterator pointing to the element preceding the first element of the deque which is considered as reverse end.

Syntax

Deque_name.crend( )

Return Value

deque_crend( ) function returns const_reverse_iterator of deque.

Example

Input Deque − 5 4 3 2 1

Output Deque in reverse order − 1 2 3 4 5

Input  Deque − 75 45 33 77 12

Output  Deque in reverse order − 12 77 33 45 75

Approach can be followed

  • First we declare the deque.

  • Then we print the deque.

  • Then we use crend( ) function.

By using above approach we can print the deque in reverse order.

Example

// C++ code to demonstrate the working of deque crend( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main ( ){
   // declaring the deque
   Deque<int> deque = { 5, 4, 3, 2, 1 };
   // print the deque
   cout<< “ Deque: “;
   for( auto x = deque.begin( ); x != deque.end( ); ++x)
      cout<< *x << “ “;
   // printing deque in reverse order
   cout<< “ Deque in reverse order:”;
   for( auto x = deque.crend( ) - 1; x >= deque.begin( ); --x)
      cout<< “ “ <<*x;
   return 0;
}

Output

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

Input - Deque: 5 4 3 2 1
Output - Deque in reverse order: 1 2 3 4 5

Example

// C++ code to demonstrate the working of crend( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main( ){
   deque<char> deque ={ ‘L’ , ‘A’ , ‘P’ , ‘T’ , ‘O’ , ‘P’ };
   cout<< “ Deque: “;
   for( auto x = deque.begin( ); x != deque.end( ); ++x)
      cout<< *x << “ “;
   // printing deque in reverse order
   cout<< “ Deque in reverse order:”;
   for( auto x = deque.crend( ) - 1; x >= deque.begin( ); --x)
      cout<< “ “ <<*x;
   return 0;
}

Output

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

Input – Deque: L A P T O P
Output – Deque in reverse order: P O T P A L

Updated on: 28-Feb-2020

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements