-
头文件
#include <queue>
-
自定义比较函数
- lambda (c++11)
注意使用关键字decltype
auto comp = [origin]( Point a, Point b ) { auto aDis = distanceSquare(a, origin); auto bDis = distanceSquare(b, origin); if (aDis == bDis) { if (a.x == b.x) { return a.y < b.y; } else { return a.x < b.x; } } else { return aDis < bDis; } }; priority_queue<Point, vector<Point>, decltype(comp)> q(comp);
- 结构体 TBD
struct cmp{ bool operator() ( Point a, Point b ){ if( a.x== b.x ) return a.y> b.y; return a.x> b.x; } }; priority_queue<Point, vector<Point>, cmp> q();
- 重载操作符
struct node { friend bool operator< (node n1, node n2) { return n1.priority < n2.priority; } int priority; int value; };
- lambda (c++11)
排列顺序
使用<
时大的内容具有较高优先级
Reference
http://blog.csdn.net/yuanjilai/article/details/8043157
https://stackoverflow.com/questions/5807735/c-priority-queue-with-lambda-comparator-error