C++ list运用实例
#include <list> #include <iostream> #include <algorithm> #include <iterator> using namespace std; void printList(const list<int>& list1,const list<int>& list2) { cout << "list1:"; copy(list1.begin(),list1.end(),ostream_iterator<int>(cout," ")); cout << endl<<"list2:"; copy(list2.begin(),list2.end(),ostream_iterator<int>(cout," ")); cout << endl<<endl; } int main() { list<int> list1, list2; for (int i=0;i<6;++i) { list1.push_back(i); list2.push_front(i); } printList(list1,list2); list2.splice(find(list2.begin(),list2.end(),3),list1); printList(list1,list2); list2.splice(list2.end(),list2,list2.begin()); printList(list1,list2); list2.sort(); list1 = list2; list2.unique(); printList(list1,list2); list1.merge(list2); printList(list1,list2); system("pause"); return 0; }
list1:0 1 2 3 4 5
list2:5 4 3 2 1 0
list1:
list2:5 4 0 1 2 3 4 5 3 2 1 0
list1:
list2:4 0 1 2 3 4 5 3 2 1 0 5
list1:0 0 1 1 2 2 3 3 4 4 5 5
list2:0 1 2 3 4 5
list1:0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
list2:
请按任意键继续. . .
代码参考:C++标准库(第2版)