优先队列

主要功能就是一个堆了 毕竟比手写方便嘛 ( ̄▽ ̄)

头文件 : include<queue>

声明:

priority_queue<int> q; 默认从大到小

如果想加入自定义的排序顺序的话 有如下几种方式:

1)

priority_queue <int,vector<int>,greater<int> > q;
priority_queue <int,vector<int>,less<int> >q;

这里不需要vector头文件

请注意 "…less<int> >… " 的空格 否则会报错

 

2)

struct cmp{
    inline bool operator () (const int &x, const int &y){
        return x < y;
    }
};
priority_queue<int, vector<int>, cmp> q;

如是,效果为从大到小排序

 

3)

struct node {     

  int w;     
  friend bool operator < (node a, node b)     
  {         
    return a.w < b.w;
  }
};

priority_queue<node> q;

如是,效果为从大到小排序

 

功能:

empty()      队列为空返回true

pop()    删除堆顶元素

push()        一个元素入堆

size()      返回元素总个数

top()     返回堆顶元素

posted @ 2018-06-15 22:21  hjmmm  阅读(185)  评论(0编辑  收藏  举报