C ++在else / if中声明一个优先级队列?

C ++在else / if中声明一个优先级队列?

问题描述:

我有两个不同的优先级队列,使用不同的比较器。我想一个if / else与一个布尔值来确定哪个p.q.将被设置为一个变量(例如pq);

I have two different priority queues that use different comparators. I would like an if/else with a boolean value to determine which p.q. will be set to a variable (for example, "pq");

例如我有

priority_queue<test, vector<test>, CompareTest1> pq;

我如何将它放置在if / else中,所以如果布尔被标记pq将被设置为...

How would I place this in an if/else so if a boolean is flagged pq will be set like...

priority_queue<test, vector<test>, CompareTest2> pq;

谢谢。

这不能实现,因为如果你给一个不同的模板参数,两个模板是不同的类型。我的建议是传递一个具有布尔值的比较函数,该布尔值由if语句中的任何内容决定。例如:

This cannot be done because if you give a different template param the two templates are different types. My suggestion would be to pass a compare functor that has a boolean which is determined by whatever was gonna be in that if statement. For example:

struct compare {
    bool b;   
    //set this instead of the if statement and allow the function to do something different
    operator () (const test & lhs, const test & rhs) {
    }
};