C++ Standard Library C++ STL Library

C++ <forward_list> - emplace_front() Function



The C++ forward_list::emplace_front function is used to insert new element at the beginning of the forward_list, before the current first element. Each insertion of element increases the forward_list container size by one.

Syntax

template <class... Args>
void emplace_front (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 forward_list::emplace_front function is used to insert new element at the beginning of the forward_list flist.

#include <iostream>
#include <forward_list>
using namespace std;
 
int main (){
  forward_list<int> flist{10, 20, 30, 40, 50};
  forward_list<int>::iterator it;
  
  //insert a new element at the end of the forward_list
  flist.emplace_front(1000);

  //insert a another new element at the end of the forward_list
  flist.emplace_front(2000);

  //insert a another new element at the end of the forward_list
  flist.emplace_front(3000);

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

  return 0;
}

The output of the above code will be:

flist contains: 3000 2000 1000 10 20 30 40 50  

❮ C++ <forward_list> Library