Ric*_*ard 7 c++ std priority-queue
这个网站建议如果我想反向排序我的优先级队列,我应该使用以下代码:
#include <iostream>
#include <queue>
using namespace std;
class mycomparison{
bool reverse;
public:
mycomparison(const bool &revparam=false) {reverse=revparam;}
bool operator() (const int &lhs, const int &rhs) const {
if (reverse) return (lhs>rhs);
else return (lhs<rhs);
}
};
int main (){
int myints[]= {10,60,50,20};
priority_queue<int, vector<int>, mycomparison(true)> first;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这困扰我:
是否有更优雅或更简洁的方式对优先级队列进行反向排序?
jua*_*nza 19
您无法避免指定存储容器,但可以避免编写自己的仿函数:
priority_queue<int, vector<int>, std::greater<int> > first;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
20876 次 |
最近记录: |