deque push_back( ) in C++ in STL

C++Server Side ProgrammingProgramming

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

The push_back( ) function is used to insert the new element into the deque ay the end

Syntax

dequename.push_front(value)

Parameters

value − It defines the new element which to be inserted at the back of deque.

Example

Input Deque − 45 46 47 48 49

Output New Deque − 45 46 47 48 49 50

Input Deque − B L A N K E T

Output New Deque − B L A N K E T S

Approach can be followed

  • First we declare the deque.

  • Then we print the deque.

  • Then we define the push_back( ) function.

By using above approach we can insert new element at the back of deque. New element should have same data type as deque.

Example

// C++ code to demonstrate the working of deque push_back( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main ( ){
   // initializing the deque
   Deque<int> deque = { 71, 75, 73, 76, 77 };
   // print the deque
   cout<< “ Deque: “;
   for( auto x = deque.begin( ); x != deque.end( ); ++x)
      cout<< *x << “ “;
   // defining the push_backt( ) function
   deque.push_back(78);
   // printing new deque after inserting new element
   for( x = deque.begin( ); x != deque.end( ); ++x)
      cout<< “ “ << *x;
   return 0;
}

Output

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

Input - Deque: 71 75 73 76 77
Output - New Deque:71 75 73 76 77 78
Input – Deque: B R E A K
Output – New Deque: B R E A K S

Example

// C++ code to demonstrate the working of deque push_back( ) function
#include<iostream.h>
#include<deque.h>
Using namespace std;
int main( ){
   // initializing deque
   deque<int> deque ={ 64, 65, 66, 69, 68 };
   cout<< “ Deque: “;
   for( auto x = deque.begin( ); x != deque.end( ); ++x)
      cout<< *x << “ “;
   // defining the push_back( ) function
   deque.push_back(67);
   // printing new deque after inserting new element
   for(x = deque.begin( ); x != deque.end( ); ++x)
      cout<< “ “ << *x;
   return 0;
}

Example

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

Input: 64 65 66 69 68
Output: 64 65 66 69 68 67

Input: T U T O R I A L
Output: T U T O R I A L S
raja
Published on 26-Feb-2020 15:49:17
Advertisements