【发布时间】:2020-05-13 13:03:48
【问题描述】:
我正在尝试根据用户需求重新排序 priority_queue。
这是数据结构:
struct Person {
int age;
float height;
};
我单独使用这个结构来以递减的方式重新排序
struct CompareHeight {
bool operator()(Person const& p1, Person const& p2)
{
// return "true" if "p1" is ordered
// before "p2", for example:
return p1.height > p2.height;
}
} HI;
我单独使用这个结构来以递增的方式重新排序
struct CompareHeightInv {
bool operator()(Person const& p1, Person const& p2)
{
// return "true" if "p1" is ordered
// before "p2", for example:
return p1.height < p2.height;
}
} HD;
我每个人都通过以下方式称呼每个人:
priority_queue<Person, vector<Person>, CompareHeightInv> inc;
priority_queue<Person, vector<Person>, CompareHeight > dec;
我的问题是:有没有这样的方法
class Foo {
private:
...
priority_queue<Person, vector<Person>, something> myQueue;
...
public:
Foo (bool flag) {
if (flag)
myQueue is increasing
else
myQueue is deacreasing
}
}
【问题讨论】:
-
编辑你的问题,而不是评论
-
如果
str是constexpr,那么你可以使用if constexpr。如果没有,那么您将需要生成您的代码,使其与优先级队列无关。如果str是constexpr,也可以使用std::conditional_t<str == "increasing", CompareHeightInv, CompareHeight>
标签: c++ pointers struct priority-queue