C++ Standard Library C++ STL Library

C++ <deque> - push_front() Function



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

Syntax

void push_front (const value_type& val);
void push_front (const value_type& val);
void push_front (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_front function is used to add a new element at the beginning 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_front(10);
  MyDeque.push_front(20);
  MyDeque.push_front(30);
  MyDeque.push_front(40);
  MyDeque.push_front(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: 50 40 30 20 10

❮ C++ <deque> Library

5