65,211
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <list>
using namespace std;
int main()
{
int array[] = {1, 2, 3, 4, 5, 6, 7, 8};
list<int> ilist(array, array + 8);
list<int>::iterator lter = ilist.begin(), beg = ilist.begin(), end = ilist.end();
++beg;
ilist.splice(lter, beg, end);
for (lter = ilist.begin(); lter != ilist.end(); ++lter)
cout << *lter << " ";
cout << endl;
return 0;
}
#include <iostream>
#include <list>
using namespace std;
int main() // splice(iterator position, list <T,Allocator>& x, iterator first, iterator last);
{
int array[] = {1, 2, 3, 4, 5, 6, 7, 8};
list<int> ilist(array, array + 8);
list<int>::iterator lter = ilist.begin(), beg = ilist.begin(), end = ilist.end();
++lter; // lter 指向 2
++beg;
++beg; // beg 指向 3
--end;
--end;
--end; // end 指向 6,由于左闭合性,故 beg-end 包含:3 4 5
ilist.splice(lter, ilist, beg, end); // 把 beg与end间的元素,放到lter的前面
for (lter = ilist.begin(); lter != ilist.end(); ++lter)
cout << *lter << " "; // 1 3 4 5 2 6 7 8
cout << endl;
return 0;
}
#include <iostream>
#include <list>
using namespace std;
int main()
{
int array[] = {1, 2, 3, 4, 5, 6, 7, 8};
list<int> ilist(array, array + 8);
list<int> ilist2;
list<int>::iterator lter = ilist.begin(), beg = ilist.begin(), end = ilist.end();
++beg;
ilist.splice(lter, ilist2, end);
for (lter = ilist.begin(); lter != ilist.end(); ++lter)
cout << *lter << " ";
cout << endl;
return 0;
}