Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


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 below example, the forward_list::emplace_front function is used to insert new element at the beginning of the forward_list f_list.

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

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

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

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

  return 0;
}

The output of the above code will be:

f_list contains: 3000 2000 1000 10 20 30 40 50  

❮ C++ - Forward List