list::splice
从源列表中删除元素并将其插入到目标列表中。
// insert the entire source list void splice( const_iterator Where, list<Type, Allocator>& Source ); void splice( const_iterator Where, list<Type, Allocator>&& Source ); // insert one element of the source list void splice( const_iterator Where, list<Type, Allocator>& Source, const_iterator Iter ); void splice( const_iterator Where, list<Type, Allocator>&& Source, const_iterator Iter ); // insert a range of elements from the source list void splice( const_iterator Where, list<Type, Allocator>& Source, const_iterator First, const_iterator Last ); void splice( const_iterator Where, list<Type, Allocator>&& Source, const_iterator First, const_iterator Last );
参数
Where
目标列表中要在其前面进行插入的位置。Source
要插入目标列表中的源列表。Iter
要从源列表中进行插入的元素。First
要从源列表中进行插入的范围中的第一个元素。Last
要从源列表中进行插入的范围中的最后一个元素之外的第一个位置。
备注
第一对成员函数在 Where 所引用的位置之前,将源列表中的所有元素插入到目标列表中,然后从源列表中删除所有元素。 (&Source 不得等于 this。)
第二对成员函数将在 Where 所引用的目标列表中的位置之前,插入 Iter 所引用的元素,然后从源列表中删除 Iter。 (如果 Where == Iter || Where == ++Iter,则不会发生更改。)
第三对成员函数将在 Where 所引用的目标列表中的元素之前,插入由 [First,Last) 指定的范围,然后从源列表中删除该元素范围。 (如果 &Source == this,则范围 [First, Last) 不得包含 Where 所指向的元素。)
如果范围接合插入 N 个元素和 &Source != this,则类迭代器的对象会递增 N 次。
在所有情况下,迭代器、指针或引用接合的元素的引用都会保持有效状态且会转换为目标容器。
示例
// list_splice.cpp
// compile with: /EHsc /W4
#include <list>
#include <iostream>
using namespace std;
template <typename S> void print(const S& s) {
cout << s.size() << " elements: ";
for (const auto& p : s) {
cout << "(" << p << ") ";
}
cout << endl;
}
int main()
{
list<int> c1{10,11};
list<int> c2{20,21,22};
list<int> c3{30,31};
list<int> c4{40,41,42,43};
list<int>::iterator where_iter;
list<int>::iterator first_iter;
list<int>::iterator last_iter;
cout << "Beginning state of lists:" << endl;
cout << "c1 = ";
print(c1);
cout << "c2 = ";
print(c2);
cout << "c3 = ";
print(c3);
cout << "c4 = ";
print(c4);
where_iter = c2.begin();
++where_iter; // start at second element
c2.splice(where_iter, c1);
cout << "After splicing c1 into c2:" << endl;
cout << "c1 = ";
print(c1);
cout << "c2 = ";
print(c2);
first_iter = c3.begin();
c2.splice(where_iter, c3, first_iter);
cout << "After splicing the first element of c3 into c2:" << endl;
cout << "c3 = ";
print(c3);
cout << "c2 = ";
print(c2);
first_iter = c4.begin();
last_iter = c4.end();
// set up to get the middle elements
++first_iter;
--last_iter;
c2.splice(where_iter, c4, first_iter, last_iter);
cout << "After splicing a range of c4 into c2:" << endl;
cout << "c4 = ";
print(c4);
cout << "c2 = ";
print(c2);
}
要求
标头:<list>
命名空间: std