C++ forward_list 庫 - splice_after() 函式



描述

C++ 函式 std::forward_list::splice_after() 將範圍內的元素轉移到firstlastx*this。這些元素將插入到由position.

指向的元素之後。

宣告

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

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

C++11

  • 引數

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

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

  • first − 輸入迭代器,指向範圍內的起始位置。

last − 輸入迭代器,指向範圍內的結束位置。

返回值

異常

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

時間複雜度

線性,即 O(n)

示例

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

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

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

   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.