C++ 列表庫 - splice() 函式



描述

C++ 函式 std::list::splice() 將範圍內的元素firstlastx轉移到 *this,使用移動語義。這些元素將插入到由position.

指向的元素之前。

宣告

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

void splice (const_iterator position, list&& x, const_iterator first, const_iterator last);

C++11

  • 引數

  • position - 列表中要插入新元素的位置。

  • x - 另一個相同型別的列表物件。

  • first - 範圍初始位置的輸入迭代器

last - 範圍結束位置的輸入迭代器

返回值

異常

如果提供的範圍無效,則行為未定義。

時間複雜度

線性,即 O(n)

示例

#include <iostream>
#include <list>

using namespace std;

int main(void) {
   list<int> l1 = {1, 2};
   list<int> l2 = {3, 4, 5};

   l1.splice(l1.end(), move(l2), l2.begin(), l2.end());

   cout << "Contents of list l1 after splice operation" << endl;

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

   return 0;
}

線上演示

Contents of list l1 after splice operation
1
2
3
4
5
讓我們編譯並執行以上程式,這將產生以下結果:
列印頁面
© . All rights reserved.