在线文档教程
C++
容器 | Containers

std::list::splice

STD::列表::拼接

void splice( const_iterator pos, list& other (1)
void splice( const_iterator pos, list&& other (1)(since C++11)
void splice( const_iterator pos, list& other, const_iterator it (2)
void splice( const_iterator pos, list&& other, const_iterator it (2)(since C++11)
void splice( const_iterator pos, list& other, const_iterator first, const_iterator last(3)
void splice( const_iterator pos, list&& other, const_iterator first, const_iterator last (3)(since C++11)

将元素从一个列表转移到另一个列表。

没有复制或移动元素,只重新指向列表节点的内部指针。如果:get_allocator() != other.get_allocator()没有迭代器或引用失效,对移动元素的迭代器仍然有效,但现在引用到*this,而不是进入other...

1%29从other*this.将元素插入到pos.集装箱other手术后变为空。如果this == &other...

2%29传输由itother*this。元素插入到pos...

3%29转移范围内的元素[first, last)other*this.将元素插入到pos如果pos是范围内的迭代器。[first,last)...

参数

pos-element before which the content will be inserted
other-another container to transfer the content from
it-the element to transfer from other to *this
first, last-the range of elements to transfer from other to *this

返回值

%280%29

复杂性

1-2%29常数。

3%29常数this == &other,否则线性在std::distance(first, last)...

二次

#include <iostream> #include <list> std::ostream& operator<<(std::ostream& ostr, const std::list<int>& list) { for (auto &i : list) { ostr << " " << i; } return ostr; } int main () { std::list<int> list1 = { 1, 2, 3, 4, 5 }; std::list<int> list2 = { 10, 20, 30, 40, 50 }; auto it = list1.begin( std::advance(it, 2 list1.splice(it, list2 std::cout << "list1: " << list1 << "\n"; std::cout << "list2: " << list2 << "\n"; list2.splice(list2.begin(), list1, it, list1.end() std::cout << "list1: " << list1 << "\n"; std::cout << "list2: " << list2 << "\n"; }

二次

产出:

二次

list1: 1 2 10 20 30 40 50 3 4 5 list2: list1: 1 2 10 20 30 40 50 list2: 3 4 5

二次

另见

mergemerges two sorted lists (public member function)
removeremove_ifremoves elements satisfying specific criteria (public member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/容器/list/splice