优先队列
以结构体为例
struct Time{
int start, end;
};
使用优先队列时,如果需要对Time中的start从小到大排序,有两种方法:
priority_queue<Time> pq;
一.在结构体外重载结构体小于运算符:
bool operator <(const Time& a,const Time& b){
return a.start > b.start;
} //这里以大于重载小于是因为默认情况下,优先队列是以大的作为队首,这样一反,就可以再默认情况下使得小的作为队首
二.直接在结构体中重载小于运算符
struct Time{
int start, end;
bool operator < (const Time& t)const{
return start > t.start;
}
};
三.
struct node{
int num,index,cnt;
node(int _num=0,int _index=0,int _cnt=0):num(_num),index(_index),cnt(_cnt){
}
bool friend operator < (node a,node b)