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

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

1 2 3 4 5 6

Time Complexity : O(1)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.