cpp的stl的队列和栈

cpp的stl的队列和栈

stl的queue和stack除了第二个参数,可以指定采用哪个集合存储数据,其他的看代码即可了解

//
//  main.cpp
//  use_stack_queue
//
//  Created by bikang on 16/11/1.
//  Copyright (c) 2016年 bikang. All rights reserved.
//

#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <list>

using namespace std;


void tstack();
void tqueue();


int main(int argc, const char * argv[]) {
    //tstack();
    tqueue();
    return 0;
}

void tstack(){
    //stack处理
    stack<int> ps;
    for(int i=0;i<10;++i)ps.push(i);
    cout << ps.size() << endl;
    cout << ps.top() << endl;
    ps.pop();
    cout << ps.top() << endl;

    //第二个参数表示用什么来存储数据
    stack<int, vector<int>> ps2;
}

void tqueue(){
    cout << "test"<<endl;
    queue<int,list<int>> q1;
    for(int i=0;i<10;++i)q1.push(i);
    cout << q1.size() <<endl;
    cout << q1.front() << " " << q1.back() << endl;
    q1.pop();
    cout << q1.front() << " " << q1.back() << endl;

    //优先堆
    priority_queue<int> p1;
    p1.push(31);
    p1.push(2);
    p1.push(32);
    p1.push(18);
    cout << p1.size()<< endl;
    while (!p1.empty()) {
        cout << p1.top()<<",";
        p1.pop();
    }
    cout << endl;

    //使用谓词从小到大排序
    priority_queue<int,vector<int>,greater<int>> p2;
    p2.push(31);
    p2.push(2);
    p2.push(32);
    p2.push(18);
    cout << p2.size()<< endl;
    while (!p2.empty()) {
        cout << p2.top()<<",";
        p2.pop();
    }
    cout << endl;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值