C++ List 库 - splice() 函数
描述
C++ 函数 std::list::splice() 将迭代器 i 指向的元素从列表 x 转移到 *this。该元素插入到 position 指向的元素之前。
声明
以下是 std::list::splice() 函数形式 std::list 头的声明。
C++98
void splice (iterator position, list& x, iterator i);
C++11
void splice (const_iterator position, list& x, const_iterator i);
参数
position − 列表中要插入新元素的位置。
x − 另一个相同类型的列表对象。
i − 随机访问迭代器。
返回值
None.
异常
如果迭代器指向无效位置,则行为 undefined。
时间复杂度
常数,即 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(), 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

