C++ List::emplace_back() 函数



C++ 的std::list::emplace_back() 函数用于在列表的末尾追加一个新元素。

它将指定的值插入到当前列表的末尾,并将列表大小增加一。emplace_back() 函数的返回类型为 void,这意味着它不返回值。它类似于 C++ std::list 中的 push_back() 函数。

push_back() 函数将给定元素追加到列表的末尾。它首先通过调用构造函数创建一个临时对象,而 emplace_back() 则在容器末尾插入一个元素,而无需创建任何临时对象。

语法

以下是 std::list::emplace_back() 函数的语法:

void emplace_back (val);

参数

  • val - 要插入到列表中的新元素值。

返回值

此函数不返回值。

示例 1

在下面的程序中,我们使用 C++ std::list::emplace_back() 函数在当前列表 {10, 20, 30, 40} 的末尾插入一个新元素值 50。

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

输出

以上程序产生以下输出:

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

示例 2

除了 int 元素之外,您还可以将 char 元素插入列表(类型为 char)。

以下是 C++ std::list::emplace_back() 函数的另一个示例。这里,我们创建一个名为 vowels 的列表(类型为 char),其值为 {'a', 'e', 'i', 'o'}。然后,使用 emplace_back() 函数,我们尝试在此列表的末尾插入一个新的 char 元素 'u'。

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

输出

执行以上程序后,它将生成以下输出:

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

示例 3

在列表(类型为 string)中插入字符串元素。

在此示例中,我们创建一个名为 fruits 的列表(类型为 string),其值为 {"Orange", "Banana", "Grapes", "Apple"}。然后,使用 emplace_back() 函数,我们尝试在此列表的末尾插入字符串元素 "Papaya"。

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

输出

这将生成以下输出:

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

示例 4

在以下示例中,我们创建一个名为 numbers 的列表(类型为 int),其值为 {1,2,3,4,5}。然后,我们在 for 循环内使用 emplace_back() 函数在当前列表的末尾动态插入数字。

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

输出

以下是上述程序的输出:

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
广告

© . All rights reserved.