deque front( ) and deque back( ) in C++ in STL

C++Server Side ProgrammingProgramming

Given is the task to show the functionality of deque front( ) and deque back( ) 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 deque front( ) function

The front( ) function is used to reference the first element of deque.

Syntax

dequename.front( )

Example

Input Deque: 12 13 14 15 16

Output New Deque: 12

Input Deque: C A P T U R E

Output New Deque: C

Approach can be followed

• First we declare the deque

• Then we print the deque.

• Then we define the front( ) function.

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

Example

// C++ code to demonstrate the working of deque front( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main ( ){
// initializing the deque
Deque<int> deque = { 5, 7, 6, 8, 9 };
// print the deque
cout<< “ Deque: “;
for( auto x = deque.begin( ); x != deque.end( ); ++x)
cout<< *x << “ “;
// defining the front( ) function
cout<< deque.front( );
return 0;
}

Output

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

Input  – Deque: 5 7 6 8 9
Output – New Deque: 5
Input  – Deque: L O N D O N
Output – New Deque: L
Published on 26-Feb-2020 11:24:54