
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Deque Rend in C++ STL
Given is the task to show the functionality of Deque rend( ) 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 rend( ) function?
The rend( ) function returns a reverse iterator pointing to the element which is preceding the first element in the deque container, the rend( ) function reverse the deque.
Syntax − deque_name.rend( )
Return Value − It returns a reverse iterator which points to the position before the first element of the deque.
Example
Input Deque − 5 4 4 2 0
Output Reversed Deque − 0 2 4 4 5
Input Deque − R E C T I F I E R
Output Reversed Deque − G O L D E N
Approach can be followed
First we declare the deque.
Then we print the deque.
Then we use the rend( ) 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 rend( ) function #include<iostream.h> #include<deque.h> Using namespace std; int main ( ){ // initializing the deque Deque<int> deque = { 7, 4, 0, 3, 7 }; // 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: 7 4 0 3 7 Output - Reversed Deque: 7 3 0 4 7
Example
// C++ code to demonstrate the working of deque rend( ) function #include<iostream.h> #include<deque.h> Using namespace std; int main( ){ // initializing deque deque<char> deque ={ ‘S’ , ‘U’ , ‘B’ , ‘T’ , ‘R’ , ‘A’ , ‘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: S U B T R A C T Output – Reversed deque : T C A R T B U S