C++ Standard Library C++ STL Library

C++ <deque> - push_back() Function



The C++ deque::push_back function is used to add a new element at the end of the deque. Addition of new element always occurs after its current last element and every addition results into increasing the container size by one.

Syntax

void push_back (const value_type& val);
void push_back (const value_type& val);
void push_back (value_type&& val);

Parameters

val Specify the new element which need to be added in the deque.

Return Value

None.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the deque::push_back function is used to add a new element at the end of the deque called MyDeque.

#include <iostream>
#include <deque>
using namespace std;
 
int main (){
  deque<int> MyDeque;
  deque<int>::iterator it;

  //add new elements in the deque
  MyDeque.push_back(10);
  MyDeque.push_back(20);
  MyDeque.push_back(30);
  MyDeque.push_back(40);
  MyDeque.push_back(50);


  cout<<"The deque contains:";
  for(it = MyDeque.begin(); it != MyDeque.end(); ++it)
    cout<<" "<<*it;

  return 0;
}

The output of the above code will be:

The deque contains: 10 20 30 40 50

❮ C++ <deque> Library