(1)基本数据类型的优先级设置
priority_queue< int> q;
priority_queue< int, vector< int>, less< int> > q;
一定要注意最后两个>之间有一个空格,否则编译时会报错 例如:error C2146: syntax error : missing ‘,’ before identifier ‘q’
这两种优先队列是等价的,less< int >表示数字大的优先级高,而greater正好相反
priority_queue< int, vector< int >, greater< int > > q;
(2) 结构体的优先级设置
struct House {
int number;// 楼栋号
int price; //价格每平米
};
如果你希望楼价高的优先级高,那么就需要重载小于号“<”
struct House {
int number;
int price;
friend bool operator < (House h1, House h2) {
return h1.price < h2.price;
}
};
如果希望价格低的优先级高,则
struct House {
int number;
int price;
friend bool operator < (House h1,House h2) {
return h1.price > h2.price;
}
};
还可以重写cmp函数
C++ priority_queue内元素优先级的设置以及注意事项
最新推荐文章于 2023-03-02 10:57:44 发布