//方法1,运算符重载
struct cmp1 {
int x;
cmp1(int a) { x = a; }
bool operator<(const cmp1& a) const {
return x < a.x; //大根堆
}
};
//方法2;重写仿函数
struct cmp2 {
bool operator ()(cmp1 a, cmp1 b) {
return a.x < b.x; //大根堆
}
};
//方法3
static bool cmp3(cmp1 a, cmp1 b) {
return a.x > b.x; //小根堆
}
void testPriorityQueue() {
cmp1 a(1);
cmp1 b(2);
cmp1 c(3);
//priority_queue<tmp1> q; //方法1,大根堆
//priority_queue<tmp1,vector<tmp1>,cmp2>q; //方法2,大根堆
priority_queue<cmp1, vector<cmp1>, decltype(&cmp3)> q(cmp3); //方法3,小根堆
q.push(b);
q.push(c);
q.push(a);
while(q.size()){
cout << q.top().x << endl;
q.pop();
}
}
c++ priority_queue 存储自定义数据
最新推荐文章于 2023-04-01 16:54:02 发布