C++的STL之容器<2>

来源:互联网 发布:java初始化容量 编辑:程序博客网 时间:2024/06/17 08:26

3、list

#include <iostream>#include <list>#include <stdlib.h>using namespace std;int main1(void)      //list 的输出{list<int>Mylist;Mylist.push_back(1);Mylist.push_back(2);Mylist.push_back(3);Mylist.push_back(4);//Mylist[3];   //链式表不能下标访问//输出           迭代式访问auto ibegin = Mylist.begin();auto iend = Mylist.end();for (; ibegin != iend; ibegin++){cout << *ibegin << "  ";}cin.get();return 0;}int main2(void)      //删除{list<int>Mylist;Mylist.push_back(10);Mylist.push_back(20);Mylist.push_back(30);Mylist.push_back(40);//删除头Mylist.erase(Mylist.begin());//Mylist.erase(Mylist.begin() + 3);     //报错auto i = Mylist.begin();    //自动类型定义i++;Mylist.erase(i);auto ibegin = Mylist.begin();auto iend = Mylist.end();for (; ibegin != iend; ibegin++){cout << *ibegin << "  ";}cin.get();return 0;}int main3(void)       //删除{list<int>Mylist;Mylist.push_back(10);Mylist.push_back(20);Mylist.push_back(30);Mylist.push_back(40);Mylist.push_back(50);auto ibegin = Mylist.begin();auto iend = Mylist.end();for (; ibegin != iend; ibegin++){if ((*ibegin) == 30){Mylist.erase(ibegin);      //不能在这里输出 ,会出错break;}}{               //块语句中输出auto ibegin = Mylist.begin();auto iend = Mylist.end();for (; ibegin != iend; ibegin++){cout << *ibegin << "  ";}}cin.get();return 0;}int main4(void){int a[5] = { 1,2,3,4,5 };list<int>Mylist(a, a + 5);      //用数组初始化Mylist.push_front(1000);        //头插Mylist.push_back(10000);          //尾插Mylist.remove(5);            //直接删除数据为 5 的auto ibegin = Mylist.begin();auto iend = Mylist.end();for (; ibegin != iend; ibegin++){cout << *ibegin << "   ";}cout << "\n";//插入{auto ibegin = Mylist.begin();auto iend = Mylist.end();for (; ibegin != iend; ibegin++){if ((*ibegin) == 4){Mylist.insert(ibegin,10);   //在4的位置插入10 ,4往后移动break;}}}//输出{auto ibegin = Mylist.begin();auto iend = Mylist.end();for (; ibegin != iend; ibegin++){cout << *ibegin << "   ";}}cin.get();return 0;}int main5(void){int a[5] = { 1,2,3,4,5 };list<int>Mylist(a, a + 5);   //反向迭代器auto rbegin = Mylist.rbegin();auto rend = Mylist.rend();for (; rbegin != rend; rbegin++){cout << *rbegin << "  ";}cin.get();return 0;}int main6(void)                        //归并排序{int a[5] = { 1,2,3,4,5 };list<int>Mylist1(a, a + 5);int b[5] = { 10,20,30,40,50 };list<int>Mylist2(b, b + 5);Mylist1.sort();      //排序Mylist2.sort();      //排序         //依赖于两个链表的排序Mylist1.merge(Mylist2);     //归并排序  把Mylist2归并到Mylist1中{auto ibegin = Mylist1.begin();auto iend = Mylist1.end();for (; ibegin != iend; ibegin++){cout << *ibegin << "  ";      //输出全部的}}cout << "\n\n\n";{auto ibegin = Mylist2.begin();auto iend = Mylist2.end();for (; ibegin != iend; ibegin++){cout << *ibegin << "  ";          //不输出}}cin.get();return 0;}int main(void){int a[5] = { 11,11,22,4,5 };list<int>Mylist(a, a + 5);{auto ibegin = Mylist.begin();auto iend = Mylist.end();for (; ibegin != iend; ibegin++){cout << *ibegin << "  ";      }}cout << "\n排序后:\n";Mylist.sort();{auto ibegin = Mylist.begin();auto iend = Mylist.end();for (; ibegin != iend; ibegin++){cout << *ibegin << "  ";}}cout << "\n\n";Mylist.unique();          //剔除重复的数值  ,同样依赖于排序sort{auto ibegin = Mylist.begin();auto iend = Mylist.end();for (; ibegin != iend; ibegin++){cout << *ibegin << "  ";}}cin.get();return 0;}