C++ forward_list::emplace_after() 函式



C++ 的std::forward_list::emplace_after()函式用於在forward_list容器中指定位置之後插入(或附加)一個新元素。

它返回一個指向新插入元素的迭代器,並將forward_list的大小增加一。在C++中,迭代器是一個可以遍歷STL(標準模板庫)容器中的元素並訪問單個元素的物件。

我們可以使用emplace_after()函式以及for迴圈在forward_list容器中指定位置之後動態插入元素。

語法

以下是C++ std::forward_list::emplace_after()函式的語法:

iterator emplace_after (val, pos);

引數

  • val - 要插入到forward_list中的新元素的值。
  • position - forward_list中新元素要插入到的位置。

返回值

此函式返回一個指向新插入元素的迭代器。

示例1

在開頭位置之後插入一個元素。

在下面的程式中,我們使用C++ std::forward_list::emplace_after()函式在當前forward_list {10, 30, 40, 50}的開頭位置之後插入一個新的指定元素20。

#include<iostream>
#include<forward_list>
using namespace std;
int main() {
   //create a forward_list
   forward_list<int> num_list = {10, 30, 40, 50};
   cout<<"The num_list contents before the emplace_after operation: "<<endl;
   for(int n : num_list){
      cout<<n<<endl;
   }
   //value of element and pos
   int val = 20;
   auto pos = num_list.begin();
   //using the emplace_after() function 
   num_list.emplace_after(pos, val);
   cout<<"The num_list contents after the emplace_after operation: "<<endl;
   for(int n : num_list){
      cout<<n<<endl;
   }
}

輸出

以下是上述程式的輸出:

The num_list contents before the emplace_after operation: 
10
30
40
50
The num_list contents after the emplace_after operation: 
10
20
30
40
50

示例2

如果forward_list是字元型別,則此函式會在指定位置之後插入新的指定元素。

以下是C++ std::forward_list::emplace_after()函式的另一個示例。在這裡,我們建立一個名為char_list的forward_list(字元型別),其內容為{'B', 'C', 'D', 'E', 'F'}。然後,使用emplace_after()函式,我們嘗試在此forward_list的指定位置之後插入一個新的指定元素'A'。

#include<iostream>
#include<forward_list>
using namespace std;
int main() {
   //create a forward_list
   forward_list<char> char_list = {'B', 'C', 'D', 'E', 'F'};
   cout<<"The char_list contents before the emplace_after operation: "<<endl;
   for(char c : char_list){
      cout<<c<<endl;
   }
   //value of element and pos
   char val = 'A';
   auto pos = char_list.before_begin();
   //using the emplace_after() function 
   char_list.emplace_after(pos, val);
   cout<<"The char_list contents after the emplace_after operation: "<<endl;
   for(char c : char_list){
      cout<<c<<endl;
   }
}

輸出

這將生成以下輸出:

The char_list contents before the emplace_after operation: 
B
C
D
E
F
The char_list contents after the emplace_after operation: 
A
B
C
D
E
F

示例3

除了int型別和char型別元素外,我們還可以將字串型別元素插入到指定位置之後。

在這個例子中,我們建立一個名為names的forward_list(字串型別),其內容為{"Aman", "Abhishek", "Ganesh", "Mohit"}。然後,使用emplace_after()函式,我們嘗試在當前forward_list的開頭位置之後插入一個新的指定元素"Raju"。

#include<iostream>
#include<forward_list>
using namespace std;
int main() {
   //create a forward_list
   forward_list<string> names = {"Aman", "Abhishek", "Ganesh", "Mohit"};
   cout<<"The names forward_list contents before the emplace_after operation: "<<endl;
   for(string s: names){
      cout<<s<<endl;
   }
   //value of element and pos
   string val = "Raju";
   auto pos = names.begin();
   //using the emplace_after() function 
   names.emplace_after(pos, val);
   cout<<"The names forward_list contents after the emplace_after operation: "<<endl;
   for(string s : names){
      cout<<s<<endl;
   }
}

輸出

上述程式產生以下輸出:

The names forward_list contents before the emplace_after operation: 
Aman
Abhishek
Ganesh
Mohit
The names forward_list contents after the emplace_after operation: 
Aman
Raju
Abhishek
Ganesh
Mohit

示例4

使用emplace_after()函式以及for迴圈在當前forward_list的指定位置之後動態插入元素。

#include<iostream>
#include<forward_list>
using namespace std;
int main() {
   //create a forward_list
   forward_list<int> num_list = {1};
   cout<<"The size of the num_list before the emplace_after operation: "<<endl;
   int size = distance(num_list.begin(), num_list.end());
   cout<<size<<endl;
   //pos
   auto pos = num_list.begin();
   for(int i = 10; i>1; i--){
      num_list.emplace_after(pos, i);
   }
   cout<<"The num_list contents after the emplace_after operation: "<<endl;
   for(int n : num_list){
      cout<<n<<endl;
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

The size of the num_list before the emplace_after operation: 
1
The num_list contents after the emplace_after operation: 
1
2
3
4
5
6
7
8
9
10
廣告
© . All rights reserved.