C++ forward_list 庫 - splice_after() 函式



描述

C++ 函式 std::forward_list::splice_after() 將迭代器指向的元素i從 forward_listx轉移到*this。該元素插入到由position.

指向的元素之後。

宣告

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

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

C++11

  • 引數

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

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

i − 隨機訪問迭代器。

返回值

異常

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

時間複雜度

線性,即 O(n)

示例

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

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

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

   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
讓我們編譯並執行上述程式,這將產生以下結果:
列印頁面
廣告
© . All rights reserved.