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

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of list::emplace_front() and list::emplace_back() function in C++ STL.

What is a List in STL?

List is a data structure that allows constant time insertion and deletion anywhere in sequence. Lists are implemented as doubly linked lists. Lists allow non-contiguous memory allocation. List performs better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards.

What is the list::emplace_front()?

list::emplace_front() is an inbuilt function in C++ STL which is declared in <list> header file. emplace_front() is used to emplace (insert) the element at the beginning of the list container. If the container is empty, it pushes the element at the first position and the element becomes the first element and if the container is having the elements beforehand the function inserts the element passed to it to the front and the existing element which is on first position will become the second element. This function increases the size of the container by 1.

Syntax

listname.emplace_front (const value_type& element1);
listname.emplace_front (value_type&& element1);

Parameter

This function accepts only 1 element which is to be emplaced/inserted.

Return Value

This function returns nothing.

Example

Input: list<int> mylist = {1, 2, 3, 4};
mylist.emplace_front(0)
Output:
List elements are = 0 1 2 3 4

Example

 Live Demo

#include <iostream>
#include <list>
using namespace std;
int main(){
   list<int> List;
   List.emplace_front(10);
   List.emplace_front(20);
   List.emplace_front(30);
   List.emplace_front(40);
   List.emplace_front(50);
   List.emplace_front(60);
      cout<<"Elements are : ";
   for(auto i = List.begin(); i!= List.end(); ++i)
      cout << ' ' << *i;
   return 0;
}

Output

If we run the above code it will generate the following output −

Elements are : 60 50 40 30 20 10

What is the list::emplace_back()?

list::emplace_back() is an inbuilt function in C++ STL which is declared in <list> header file. emplace_back() is used to emplace (insert) the element at the back or at the end of the list container. If the container is empty, it just simply inserts the element and the size of the container becomes 1 if the container is having the elements beforehand the function inserts the element passed to it at the end of the list container. This function increases the size of the container by 1.

Syntax

listname.emplace_back(const value_type& element1);
listname.emplace_back(value_type&& element1);

Parameter

This function accepts only 1 element which is to be emplaced/inserted.

Return Value

This function returns nothing.

Example

Input: list<int> list1 = {1, 2, 3, 4};
   list1.emplace_back(5);
Output: List: 1 2 3 4 5

Example

 Live Demo

#include <iostream>
#include <list>
using namespace std;
int main(){
   list<int> List;
   List.emplace_back(10);
   List.emplace_back(20);
   List.emplace_back(30);
   List.emplace_back(40);
   List.emplace_back(50);
   List.emplace_back(60);
   cout<<"elements are : ";
   for(auto i=List.begin(); i!= List.end(); ++i)
      cout << ' ' << *i;
   return 0;
}

Output

If we run the above code it will generate the following output −

Elements are : 10 20 30 40 50 60
raja
Published on 05-Mar-2020 15:13:29
Advertisements