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



描述

C++ 函式std::list::splice() 使用移動語義將迭代器i指向的元素從列表x轉移到 *this 中。該元素插入到由position.

指向的元素之前。

宣告

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

void splice (const_iterator position, list&& x, const_iterator i);

C++11

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

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

  • i − 隨機訪問迭代器。

返回值

無。

異常

如果迭代器指向無效位置,則行為未定義。

時間複雜度

常數,即 O(1)

示例

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

#include <iostream>
#include <list>

using namespace std;

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

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

   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
list.htm
廣告