What is deque back( ) in C++?

C++Server Side ProgrammingProgramming

The deque back( ) function is used to reference last element of deque.

Syntax

dequename.back( )

Example

Input Deque − 11 12 13 14 15

Output New Deque − 15

Input Deque − C H O I C E

Output New Deque − E

Approach can be followed

  • First we declare the deque

  • Then we print the deque.

  • Then we define the back( ) function.

By using above approach we can fetch the last element of the deque.

Example

// C++ code to demonstrate the working of deque back( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main( ){
   // initializing deque
   deque<int> deque ={ 14, 15, 16, 17, 18 };
   cout<< “ Deque: “;
   for( auto x = deque.begin( ); x != deque.end( ); ++x)
      cout<< *x << “ “;
   // defining the back( ) function
   cout<< deque.back( );
   return 0;
}

Output

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

Input: 14 15 16 17 18
Output: 18
Input: S A L T
Output: T
raja
Published on 26-Feb-2020 15:57:39
Advertisements