#include<iostream>
#include<list>
#include<algorithm>
#include<iterator>
using namespace std;
void PrintLists(const list<int>& l1, const list<int>& l2){
cout << "list1: ";
copy(l1.begin(), l1.end(), ostream_iterator<int>(cout, " ") );
cout << endl;
cout << "list2: ";
copy(l2.begin(), l2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << endl;
}
int main(){
list<int> list1, list2;
for (int i = 0; i < 6; i++)
{
list1.push_back(i);
list2.push_front(i);
}
PrintLists(list1, list2);
list2.splice(find(list2.begin(), list2.end(), 3), list1);
PrintLists(list1, list2);
list2.splice(list2.end(),
list2, list2.begin());
PrintLists(list1, list2);
list2.sort();
list1 = list2;
list2.unique();
PrintLists(list1, list2);
list1.merge(list2);
PrintLists(list1, list2);
getchar();
getchar();
#include<list>
#include<algorithm>
#include<iterator>
using namespace std;
void PrintLists(const list<int>& l1, const list<int>& l2){
cout << "list1: ";
copy(l1.begin(), l1.end(), ostream_iterator<int>(cout, " ") );
cout << endl;
cout << "list2: ";
copy(l2.begin(), l2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << endl;
}
int main(){
list<int> list1, list2;
for (int i = 0; i < 6; i++)
{
list1.push_back(i);
list2.push_front(i);
}
PrintLists(list1, list2);
list2.splice(find(list2.begin(), list2.end(), 3), list1);
PrintLists(list1, list2);
list2.splice(list2.end(),
list2, list2.begin());
PrintLists(list1, list2);
list2.sort();
list1 = list2;
list2.unique();
PrintLists(list1, list2);
list1.merge(list2);
PrintLists(list1, list2);
getchar();
getchar();
}
以下为输出的结果
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: