cpp的STL之list

cpp的STL之list

特点就是个双向链表

基本操作

//
//  main.cpp
//  use_stl_list
//
//  Created by bikang on 16/10/28.
//  Copyright (c) 2016年 bikang. All rights reserved.
//

#include <iostream>
#include <list>
using namespace std;

void tlist();

bool sortFunc(const int &a,const int &b);


int main(int argc, const char * argv[]) {
    // insert code here...
    tlist();
    return 0;
}

void tlist(){

    //创建
    std::list<int> l1;
    //插入
    l1.push_front(11);
    l1.push_front(21);
    l1.push_back(3);
    //大小
    cout << l1.size()<<endl;
    //指定的位置插入
    l1.insert(l1.begin(), 2);
    //指定的位置插入多个重复的值
    l1.insert(l1.begin(), 3,0);



    //列表显示
    list<int>::iterator elem;
    for(elem=l1.begin();elem!=l1.end();++elem){
        cout << *elem <<",";
    }
    cout << endl;

    //其他插入
    list<int> l2;
    l2.push_back(1111);
    l2.insert(l2.begin(), l1.begin(),l1.end());

    //删除
    l2.erase(l2.begin());
    //范围删除
    //l2.erase(l2.begin(),l2.end());

    l2.push_back(21);

    for(elem=l2.begin();elem!=l2.end();++elem){
        cout << *elem <<",";
    }
    cout << endl;
    //反转
    l2.reverse();

    for(elem=l2.begin();elem!=l2.end();++elem){
        cout << *elem <<",";
    }
    cout << endl;

    //排序
    l2.sort();

    for(elem=l2.begin();elem!=l2.end();++elem){
        cout << *elem <<",";
    }
    cout << endl;

    //根据排序函数排序
    l2.sort(sortFunc);

    for(elem=l2.begin();elem!=l2.end();++elem){
        cout << *elem <<",";
    }
    cout << endl;




}

bool sortFunc(const int &a,const int &b){
    return (a > b);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值