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

优先队列学习

时间:2019-12-22 00:45:31      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:enc   end   -o   class   类型   turn   直接   参数类型   detail   

<!-- more -->

参考的优质博客:

? https://blog.csdn.net/c20182030/article/details/70757660

优先队列的定义是 :

1.存在的库函数:

? #include<queue>

2.一般的定义

? priosity_queue<type, cotainers<> , cmp> q; /第一个参数为参数类型, 第二参数为容器,第三个参数为比较函数. 一般可以用 greater<类型> 表示从小到大排序.

对于 优先队列与 普通排序不同:

重载优先队列时 优先队列默认排序是 大顶堆 从大到小排序

因此要使得 优先队列 默认从小到大排序 需要 定义为:

struct cmp{

? bool operator(const Node &a,const Node &b) const{

? return a.xx > b.xx; //按照小顶堆排序

? }

}

eg:

3. 常见 重载cmp代码格式

如果直接定义 为 priosity_queue<Node> q 这样默认的格式为 :这样默认安装大顶堆的排序方式


struct Node{
int d, u;
// bool operator <( const Node &x )const //这是重载小于符号
//   {
//       return d > x.d;
//   }
};
struct cmp{  //和普通的重载排序算法不同, 因为优先队列默认是
bool operator () (const Node &a,const Node &b) const{
return a.d > b.d;
}
};

 

4.优先队列用到的场景:

? 1.堆优化算法: dijkstra算法, prime算法.

 




优先队列学习

标签:enc   end   -o   class   类型   turn   直接   参数类型   detail   

原文地址:https://www.cnblogs.com/csyxdh/p/12078622.html

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