C++ Standard Library C++ STL Library

C++ <deque> - emplace_back() Function



The C++ deque::emplace_back function is used to insert new element at the end of the deque, after the current last element. Each insertion of element increases the deque container size by one.

Syntax

template <class... Args>
void emplace_back (Args&&... args);

Parameters

args Arguments forwarded to construct the new element.

Return Value

None.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the deque::emplace_back function is used to insert new element at the end of the deque called MyDeque.

#include <iostream>
#include <deque>
using namespace std;
 
int main (){
  deque<int> MyDeque{10, 20, 30, 40, 50};
  
  //insert a new element at the end of the deque
  MyDeque.emplace_back(1000);

  //insert a another new element at the end of the deque
  MyDeque.emplace_back(2000);

  //insert a another new element at the end of the deque
  MyDeque.emplace_back(3000);

  cout<<"MyDeque contains: ";
  for(int i=0; i< MyDeque.size(); i++)
    cout<<MyDeque[i]<<" ";   

  return 0;
}

The output of the above code will be:

MyDeque contains: 10 20 30 40 50 1000 2000 3000  

❮ C++ <deque> Library