English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ List emplace_back() Usage and Example

C++ List (List)

C ++ The emplace_back() function of the list inserts a new element at the end of the list, and the size of the list increases by one.

The following figure shows the working principle of this function:

Syntax

void emplace_back(value_type val);

Parameter

val:The new value to be inserted at the end of the list.

Return value

It does not return any value.

Example1

Let's look at a simple example

#include <iostream>  
#include<list>  
using namespace std;  
int main()
{
  listli={1,2,3,4};
  list::iterator itr;
  li.emplace_back(5);
 for(itr=li.begin();itr!=li.end();++itr)
  cout<<*itr<<" ";
  return 0;
 }

Output:

1 2 3 4 5

In this example, the emplace_back() function adds a new element at the end of the list, namely5.

Example2

Let's look at a simple example

#include <iostream>  
#include<list>  
using namespace std;  
int main()
{
listli={'C','+};
list::iterator itr;
for(itr=li.begin();itr!=li.end();++itr)
std::cout << *itr;
cout << '\n';
li.emplace_back('+');
for(itr=li.begin();itr!=li.end();++itr)
std::cout << *itr;
 return 0;
}

Output:

C+
C++

In this example, the emplace_back() function adds a new character at the end of the list, that is, " +

C++ List (List)