Skip to content
Related Articles

Related Articles

list::emplace_front() and list::emplace_back() in C++ STL

Improve Article
Save Article
  • Last Updated : 23 Jun, 2022
Improve Article
Save Article

Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists.

list::emplace_front()

This function is used to insert a new element into the list container, the new element is added to the beginning of the list. Syntax :

listname.emplace_front(value)
Parameters :
The element to be inserted into the list
is passed as the parameter.
Result :
The parameter is added to the
list at the beginning.

Examples:

Input  : mylist{1, 2, 3, 4, 5};
         mylist.emplace_front(6);
Output : mylist = 6, 1, 2, 3, 4, 5

Input  : mylist{};
         mylist.emplace_front(4);
Output : mylist = 4

Errors and Exceptions 1. It has a strong exception guarantee, therefore, no changes are made if an exception is thrown. 2. Parameter should be of same type as that of the container, otherwise an error is thrown. 

CPP




// CPP program to illustrate
// Implementation of emplace_front() function
#include <iostream>
#include <list>
using namespace std;
 
int main()
{
    list<int> mylist;
    mylist.emplace_front(1);
    mylist.emplace_front(2);
    mylist.emplace_front(3);
    mylist.emplace_front(4);
    mylist.emplace_front(5);
    mylist.emplace_front(6);
    // list becomes 6, 5, 4, 3, 2, 1
 
    // printing the list
    for (auto it = mylist.begin(); it != mylist.end(); ++it)
        cout << ' ' << *it;
 
    return 0;
}

Output:

6 5 4 3 2 1

Time Complexity : O(1)

list::emplace_back()

This function is used to insert a new element into the list container, the new element is added to the end of the list. Syntax :

listname.emplace_back(value)
Parameters :
The element to be inserted into the list
is passed as the parameter.
Result :
The parameter is added to the
list at the end.

Examples:

Input  : mylist{1, 2, 3, 4, 5};
         mylist.emplace_back(6);
Output : mylist = 1, 2, 3, 4, 5, 6

Input  : mylist{};
         mylist.emplace_back(4);
Output : mylist = 4

Errors and Exceptions 1. It has a strong exception guarantee, therefore, no changes are made if an exception is thrown. 2. Parameter should be of same type as that of the container, otherwise an error is thrown. 

CPP




// CPP program to illustrate
// Implementation of emplace_back() function
#include <iostream>
#include <list>
using namespace std;
 
int main()
{
    list<int> mylist;
    mylist.emplace_back(1);
    mylist.emplace_back(2);
    mylist.emplace_back(3);
    mylist.emplace_back(4);
    mylist.emplace_back(5);
    mylist.emplace_back(6);
    // list becomes 1, 2, 3, 4, 5, 6
 
    // printing the list
    for (auto it = mylist.begin(); it != mylist.end(); ++it)
        cout << ' ' << *it;
 
    return 0;
}

Output:

1 2 3 4 5 6

Time Complexity : O(1)

Let us see the differences in a tabular form -:

 list::emplace_front()  list::emplace_back()
1.It is used to insert a new element at the beginning of the listIt is used to insert a new element at the end of the list
2.

Its syntax is -:

emplace_front ();

Its syntax is -:

 emplace_back ();

3.It does not have any return value.It does not have any return value.
4.Its complexity is constant.Its complexity is constant.
5.Its iterator validity does not changes.Its iterator validity does not changes.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!