C++ List::emplace_back() Function



The C++ std::list::emplace_back() function is used to appends a new element at the end of the list.

It inserts the specified value at the end of the current list and increases the list size by one. The return type of the emplace_back() function is void which implies that it does not return any value. It is similar to the push_back() functions in a C++ std::list.

The push_back() function appends the given element at the end of the list. It first creates a temporary object by calling a constructor, whereas the emplace_back() inserts an element at the end of the container without creating any temporary object.

Syntax

Following is the syntax of the std::list::emplace_back() function −

void emplace_back (val);

Parameters

  • val − It is a new value of element to be inserted in the list.

Return Value

This function does not return any value.

Example 1

In the following program, we are using the C++ std::list::emplace_back() function to insert a new element value 50 at the end of the current list {10, 20, 30, 40}.

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create a list
   list<int> num_list = {10, 20, 30, 40};
   cout<<"List elements before emplace_back operation: ";
   for(int n : num_list) {
      cout<<n<<" ";
   }
   //element value
   int val = 50;
   //using emplace_back() function
   num_list.emplace_back(val);
   cout<<"\nList elements after emplace_back() operation: "<<endl;
   for(int n : num_list) {
      cout<<n<<" ";
   }
   return 0;
}

Output

The above program produces the following output −

List elements before emplace_back operation: 10 20 30 40 
List elements after emplace_back() operation: 
10 20 30 40 50

Example 2

Apart from the int element, you can also insert the char element in the list(type char).

Following is another example of the C++ std::list::emplace_back() function. Here, we are creating a list(type char) named vowels with the value {'a', 'e', 'i', 'o'}. Then, using the emplace_back() function, we are trying to insert a new char element 'u' at the end of this list.

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create a list
   list<char> vowels = {'a', 'e', 'i', 'o'};
   cout<<"List elements before emplace_back operation: ";
   for(char v : vowels) {
      cout<<v<<" ";
   }
   //element value
   char val = 'u';
   //using emplace_back() function
   vowels.emplace_back(val);
   cout<<"\nList elements after emplace_back() operation: "<<endl;
   for(char v : vowels) {
      cout<<v<<" ";
   }
   return 0;
}

Output

On executing the above program, it will generate the following output −

List elements before emplace_back operation: a e i o 
List elements after emplace_back() operation: 
a e i o u

Example 3

Insert a string element in the list(type string).

In this example, we are creating a list(type string) named fruits with the values {"Orange", "Banana", "Grapes", "Apple"}. Then, using the emplace_back() function, we are trying to insert the string element "Papaya" at the end of this list.

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create a list
   list<string> fruits = {"Orange", "Banana", "Grapes", "Apple"};
   cout<<"List elements before emplace_back operation: ";
   for(string f : fruits) {
      cout<<f<<" ";
   }
   //element value
   string val = "Papaya";
   //using emplace_back() function
   fruits.emplace_back(val);
   cout<<"\nList elements after emplace_back() operation: "<<endl;
   for(string f : fruits) {
      cout<<f<<" ";
   }
   return 0;
}

Output

This will generate the following output −

List elements before emplace_back operation: Orange Banana Grapes Apple 
List elements after emplace_back() operation: 
Orange Banana Grapes Apple Papaya 

Example 4

In the following example, we are creating a list(type int) named numbers with the values {1,2,3,4,5}. Then, we are using the emplace_back() function inside the for loop to insert numbers dynamically at the end in the current list.

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create a list
   list<int> numbers  = {1,2,3,4,5};
   cout<<"List elements before emplace_back operation: ";
   for(int n : numbers ) {
      cout<<n<<" ";
   }
   //using emplace_front() function
   for(int i = 6; i<=10; i++){
      numbers.emplace_back(i);
   }
   cout<<"\nList elements after emplace_back operation: "<<endl;
   for(int n : numbers ) {
      cout<<n<<" ";
   }
   return 0;
}

Output

Following is the output of the above program −

List elements before emplace_back operation: 1 2 3 4 5 
List elements after emplace_back operation: 
1 2 3 4 5 6 7 8 9 10
Advertisements