C++ forward_list 庫 - splice_after() 函式



描述

C++ 函式std::forward_list::splice_after() 使用移動語義將所有元素從 forward_listx轉移到*thisposition之後。操作完成後,容器x將變為空。

宣告

以下是來自 std::forward_list 標頭檔案的 std::forward_list::splice_after() 函式宣告。

C++11

void splice_after (const_iterator position, forward_list&& x);

引數

  • position − forward_list 中插入新元素之後的位置。

  • x − 另一個相同型別的 forward_list 物件。

返回值

異常

此成員函式從不丟擲異常。

時間複雜度

線性,即 O(n)

示例

以下示例演示了 std::forward_list::splice_after() 函式的用法。

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

   forward_list<int> fl1 = {2, 3, 4};
   forward_list<int> fl2 = {1, 5};

   fl2.splice_after(fl2.begin(), move(fl1));

   cout << "List contains following elements" << endl;

   for (auto it = fl2.begin(); it != fl2.end(); ++it)
      cout << *it << endl;

   return 0;
}

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

List contains following elements
1
2
3
4
5
forward_list.htm
廣告
© . All rights reserved.