STL 栈的使用

STL 中栈的使用方法(stack)

基本操作:

push(x) 将x加入栈中,即入栈操作

pop() 出栈操作(删除栈顶),只是出栈,没有返回值

top() 返回第一个元素(栈顶元素)

size() 返回栈中的元素个数

empty() 当栈为空时,返回 true


使用方法:

和队列差不多,其中头文件为:

#include <stack>


定义方法为:

stack< int>s1; // 入栈元素为 int 型
stack< string>s2; //  入队元素为string型
stack<node>s3; // 入队元素为自定义型



/**//*
*===================================*
|                                   |
|       STL中优先队列使用方法       |
|                                   |        
|       chenlie                     |
|                                   |
|       2010-3-24                   |
|                                   |
*===================================*
*/
#include <iostream>
#include <vector>
#include <queue>
using  namespace std;
int c[100];

struct cmp1
{
      bool  operator ()( int x,  int y)
    {
         return x > y; // 小的优先级高
    }
};

struct cmp2
{
     bool  operator ()( const  int x,  const  int y)
    {
         return c[x] > c[y]; 
        //  c[x]小的优先级高,由于可以在对外改变队内的值,
        
// 所以使用此方法达不到真正的优先。建议用结构体类型。
    }
};

struct node
{
     int x, y;
    friend  bool  operator < (node a, node b)
    {
         return a.x > b.x; // 结构体中,x小的优先级高
    }
};


priority_queue< int>q1;

priority_queue< int, vector< int>, cmp1>q2;

priority_queue< int, vector< int>, cmp2>q3;

priority_queue<node>q4;


queue< int>qq1;
queue<node>qq2;

int main()
{
     int i, j, k, m, n;
     int x, y;
    node a;
     while (cin >> n)
    {
         for (i = 0; i < n; i++)
        {
            cin >> a.y >> a.x;
            q4.push(a);
        }
        cout << endl;
         while (!q4.empty())
        {
            cout << q4.top().y << " " << q4.top().x << endl;
            q4.pop();
        }
     //     cout << endl;
    }
     return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值