码迷,mamicode.com
首页 > 其他好文 > 详细

优先队列

时间:2018-06-15 23:30:15      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:inline   IV   ret   back   方便   less   需要   col   队列   

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

头文件 : 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()     返回堆顶元素

优先队列

标签:inline   IV   ret   back   方便   less   需要   col   队列   

原文地址:https://www.cnblogs.com/hjmmm/p/9189180.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!