Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 16 Jan, 2018

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 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 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)

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :