记录一个菜逼的成长。。web
记下对优先队列对优先级改如何定义less
这是stl里定义的比较结构
咱们都知道用greater是小顶堆,less是大顶堆,默认是less。svg
/// One of the @link comparison_functors comparison functors@endlink.
template<typename _Tp>
struct greater : public binary_function<_Tp, _Tp, bool>
{
bool operator()(const _Tp& __x, const _Tp& __y) const
{ return __x > __y; }
};
/// One of the @link comparison_functors comparison functors@endlink.
template<typename _Tp>
struct less : public binary_function<_Tp, _Tp, bool>
{
bool operator()(const _Tp& __x, const _Tp& __y) const
{ return __x < __y; }
};
若是咱们本身定义一个结构可这样写
假设定义了一个结构ui
struct Node{
int x,y,z;
}
咱们模仿上面的比较结构本身定义一个比较结构,好比spa
struct cmp{
bool operator () (const Node& a,const Node& b) const{
return a.y < b.y;//大顶堆,,改成>是小顶堆
}
}
那么建立优先队列时这样写code
priority_queue<Node,vector<Node>,cmp>q;
咱们也能够经过重载运算符来定义优先级,好比xml
struct Node{
int x,y,z;
friend bool operator > (const Node& a,const Node& b){
return a.x > b.x; //greater,小顶堆
}
friend bool operator < (const Node& a,const Node& b){
return a.x < b.x;//less,大顶堆
}
}
咱们能够这样建立优先队列队列
priority_queue<Node,vector<Node>,greater<Node> >q;
priority_queue<Node,vector<Node>,less<Node> >q;