【STL】list的常用方法

9 篇文章 0 订阅

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值