优先队列的原理是堆,运用的是堆排序,每次只能取出堆顶元素
使用方式:
https://blog.csdn.net/pzhu_cg_csdn/article/details/79166858
https://www.cnblogs.com/flyoung2008/articles/2136485.html
1.普通优先级设置:
priority_queue<int,vector<int>,less<int> >;
priority_queue<int,vector<int>,greater<int> >;
//“> >"中间有个空格,这是固定格式
less 表示数字大的优先级高即大根堆,而 greater 表示数字小的优先级高,即小根堆。
2.结构体优先级设置
结构体内部:
struct student{
int grade;
string name;
//重载运算符,grade 值小的优先级大,即小根堆
friend operator < (student s1,student s2){
return s1.grade >s2.grade;
}
};
结构体外部:
struct fruit{
string name;
int price;
};
struct cmp{
// "<" 表示 price 大的优先级高,即大根堆
bool operator() (fruit f1,fruit f2){
return f1.price < f2.price;
}
};