1.说明
std::list<T,Allocator>::splice
C++ Containers library std::list
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)
Transfers elements from one list to another.
No elements are copied or moved, only the internal pointers of the list nodes are re-
pointed. The behavior is undefined if: get_allocator() != other.get_allocator(). No
iterators or references become invalidated, the iterators to moved elements remain valid, but now refer into *this, not into other.
1) Transfers all elements from other into *this. The elements are inserted before the
element pointed to by pos. The container other becomes empty after the operation. The
behavior is undefined if other refers to the same object as *this.
2) Transfers the element pointed to by it from other into *this. The element is inserted
before the element pointed to by pos.
3) Transfers the elements in the range [first, last) from other into *this. The elements
are inserted before the element pointed to by pos. The behavior is undefined if pos is an iterator in the range [first,last).
Parameters
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
Return value
(none)
Exceptions
Throws nothing.
Complexity
1-2) Constant.
3) Constant if other refers to the same object as *this, otherwise linear in
std::distance(first, last).
2.例子1
list::splice实现list拼接的功能。将源list的内容部分或全部元素删除,拼插入到目的list.
函数有以下三种声明:
一:void splice ( iterator position, list<T,Allocator>& x );
二:void splice ( iterator position, list<T,Allocator>& x, iterator it );
三:void splice ( iterator position, list<T,Allocator>& x, iterator first,
iterator last );
解释:
position 是要操作的list对象的迭代器
list&x 被剪的对象
对于一:会在position后把list&x所有的元素到剪接到要操作的list对象
对于二:只会把it的值剪接到要操作的list对象中
对于三:把first 到 last 剪接到要操作的list对象中
#include<bits/stdc++.h>
using namespace std;
int main()
{
list<int>li1,li2;
for(int i=1;i<=4;i++)
li1.push_back(i),li2.push_back(i+10);
list<int>::iterator it=li1.begin();
it++;
li1.splice(it,li2);//1 11 12 13 14 2 3 4
if(li2.empty()) cout<<"li2 is empty"<<endl;
li2.splice(li2.begin(),li1,it);
cout<<*it<<" chen"<<endl;
/*
li1 1 11 12 13 14 3 4
li2 2
这里的it的值还是2 但是指向的已经是li2中的了
*/
it=li1.begin();
advance(it,3);//advance即为std::advance的意思,是增加的意思,就是相当于 it=it+3;这里指向13
li1.splice(li1.begin(),li1,it,li1.end()); //13 14 3 4 1 11 12 可以发现it到li1.end()被剪贴到li1.begin()前面了
for(list<int>::iterator it=li1.begin();it!=li1.end();++it) cout<<*it<<" ";
cout<<endl;
for(list<int>::iterator it=li2.begin();it!=li2.end();++it) cout<<*it<<" ";
return 0;
}
3.例子2
#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"; // 1 2 10 20 30 40 50 3 4 5
std::cout << "list2: " << list2 << "\n"; // null
std::cout << "list2.begin(): " << *(list2.begin()) << "\n"; // randam
std::cout << "it: " << *it << "\n"; // 3
std::cout << "list1.end(): " << *(list1.end()) << "\n"; // 1
list2.splice(list2.begin(), list1, it, list1.end());
std::cout << "list1: " << list1 << "\n"; // 1 2 10 20 30 40 50
std::cout << "list2: " << list2 << "\n"; // 3 4 5
}