【STL】list的常用方法

来源:互联网 发布:js遍历set集合对象 编辑:程序博客网 时间:2024/06/06 05:27

list的常用方法一览

  • 【1】insert
  • 【2】merge
  • 【3】splice
  • 【4】erase
  • 【5】remove
  • 【6】sort
  • 【7】reverse
  • 【8】swap
  • 【9】unique

程序实例

#include <iostream>#include <list>using namespace std;bool mysort(int x,int y){    return x>y;}int main(){    int a[] = {9,4,6,3,8};    int b[] = {4,5,3,1,0};    int len = sizeof(a)/sizeof(int);    int len1 = sizeof(b)/sizeof(int);    list<int>iLi(a,a+len);    list<int>iLi1(b,b+len1);    iLi.sort();    iLi1.sort();    cout<<"iLi"<<endl;    list<int>::iterator it = iLi.begin();    for(;it != iLi.end();it++)    {        cout<<*it<<" ";    }    cout<<endl;    cout<<"iLi1"<<endl;    list<int>::iterator it1 = iLi1.begin();    for(;it1 != iLi1.end();it1++)    {        cout<<*it1<<" ";    }    cout<<endl;    cout<<"splice之后"<<endl;    iLi.splice(iLi.begin(),iLi1);    cout<<"iLi"<<endl;    it = iLi.begin();    for(;it != iLi.end();it++)    {        cout<<*it<<" ";    }    cout<<endl;    cout<<"iLi1"<<endl;    it1 = iLi1.begin();    for(;it1 != iLi1.end();it1++)    {        cout<<*it1<<" ";    }    cout<<endl;    cout<<"merge之后"<<endl;    list<int>iLi2(a,a+len);    list<int>iLi3(b,b+len1);    iLi2.sort();    iLi3.sort();    iLi2.merge(iLi3);    cout<<"iLi2"<<endl;    it = iLi2.begin();    for(;it != iLi2.end();it++)    {        cout<<*it<<" ";    }    cout<<endl;    cout<<"iLi3"<<endl;    it1 = iLi3.begin();    for(;it1 != iLi3.end();it1++)    {        cout<<*it1<<" ";    }    cout<<endl;    cout<<"reverse之后iLi2"<<endl;    iLi2.reverse();    it = iLi2.begin();    for(;it != iLi2.end();it++)    {        cout<<*it<<" ";    }    cout<<endl;    iLi2.reverse();    cout<<"sort按照逆序排列iLi2"<<endl;    iLi2.sort(mysort);    it = iLi2.begin();    for(;it != iLi2.end();it++)    {        cout<<*it<<" ";    }    cout<<endl;    cout<<"insert之后,iLi2"<<endl;    iLi2.insert(iLi2.begin(),100);    //iLi2.insert(iLi2.begin(),iLi.begin(),iLi.end());    //iLi2.insert(iLi2.begin(),2,99);    it = iLi2.begin();    for(;it != iLi2.end();it++)    {        cout<<*it<<" ";    }    cout<<endl;    cout<<"remove之后"<<endl;    iLi2.remove(99);    for(it = iLi2.begin();it != iLi2.end();it++)    {        cout<<*it<<" ";    }    cout<<endl;    cout<<"erase之后"<<endl;    //iLi2.erase(++iLi2.begin());    //iLi2.erase(iLi2.begin());    int Which = 2;int i;    for(it = iLi2.begin(),i = 0; i < Which-1;i++)    {        it++;    }    iLi2.erase(it);    for(it = iLi2.begin();it != (iLi2.end());it++)    {        cout<<*it<<" ";    }    cout<<endl;    cout<<"unique之后"<<endl;    iLi2.unique();    for(it = iLi2.begin();it != iLi2.end();it++)    {        cout<<*it<<" ";    }    cout<<endl;    cout<<"swap之后"<<endl;    iLi2.swap(iLi);    cout<<"iLi"<<endl;    for(it = iLi.begin();it != iLi.end();it++)        cout<<*it<<" ";    cout<<endl;    cout<<"iLi2"<<endl;    for(it = iLi2.begin();it != iLi2.end();it++)        cout<<*it<<" ";    cout<<endl;    system("pause");    return 0;}

结果展示

这里写图片描述

0 0
原创粉丝点击