#include <iostream> #include <list> using namespace std; void PrintList(list<int> l_int) { cout << "PrintList:"; for (list<int>::iterator it = l_int.begin();it!=l_int.end();it++) { cout << *it << " "; } cout << endl; } void PrintListR(list<int> l_int) { cout << "PrintListR:"; for (list<int>::reverse_iterator it = l_int.rbegin(); it != l_int.rend(); it++) { cout << *it << " "; } cout << endl; } int main() { list<int> l_int; l_int.push_back(1); l_int.push_front(2); PrintList(l_int); PrintListR(l_int); l_int.insert(l_int.begin(), 1, 5); //第一个元素前插入1个5 PrintList(l_int); cout << "l_int.front(): " << l_int.front() << endl; cout << "l_int.back(): " << l_int.back() << endl; l_int.push_back(5); l_int.push_back(8); cout << "执行pop前" << endl; PrintList(l_int); l_int.pop_front(); l_int.pop_back(); cout << "执行pop后" << endl; PrintList(l_int); cout << "erase" << endl; l_int.erase(++l_int.begin()); PrintList(l_int); cout << "assign" << endl; l_int.assign(9, 9); PrintList(l_int); cout << "max_size:" << l_int.max_size() << endl; cout << "size:" << l_int.size() << endl; cout << "empty:" << l_int.empty() << endl; l_int.clear(); // PrintList(l_int); l_int.push_back(4); l_int.push_back(3); l_int.push_back(7); l_int.push_back(9); l_int.push_back(1); l_int.push_back(3); l_int.push_back(2); l_int.push_back(3); l_int.push_back(7); cout << "sort" << endl; PrintList(l_int); l_int.sort(); PrintList(l_int); list<int> l_int2(l_int); cout << "list2" << endl; PrintList(l_int2); cout << "unique" << endl; l_int2.unique(); PrintList(l_int2); l_int2.clear(); l_int2.push_back(1); l_int2.push_back(14); l_int2.push_back(12); l_int2.push_back(13); list<int> l_m(l_int); list<int> l_m1(l_int2); PrintList(l_int); PrintList(l_int2); cout << "splice" << endl; l_int.splice(l_int.begin(), l_int2, l_int2.begin(), l_int2.end()); //l_int.splice(l_int.begin(), l_int2, l_int2.begin()); PrintList(l_int); PrintList(l_int2); #if 1 PrintList(l_m); PrintList(l_m1); l_m.sort(); l_m1.sort(); cout << "merge" << endl; l_m.merge(l_m1); PrintList(l_m); PrintList(l_m1); #endif getchar(); }
显示:
PrintList:2 1
PrintListR:1 2
PrintList:5 2 1
l_int.front(): 5
l_int.back(): 1
执行pop前
PrintList:5 2 1 5 8
执行pop后
PrintList:2 1 5
erase
PrintList:2 5
assign
PrintList:9 9 9 9 9 9 9 9 9
max_size:357913941
size:9
empty:0
PrintList:
sort
PrintList:4 3 7 9 1 3 2 3 7
PrintList:1 2 3 3 3 4 7 7 9
list2
PrintList:1 2 3 3 3 4 7 7 9
unique
PrintList:1 2 3 4 7 9
PrintList:1 2 3 3 3 4 7 7 9
PrintList:1 14 12 13
splice
PrintList:1 14 12 13 1 2 3 3 3 4 7 7 9
PrintList:
PrintList:1 2 3 3 3 4 7 7 9
PrintList:1 14 12 13
merge
PrintList:1 1 2 3 3 3 4 7 7 9 12 13 14
PrintList: