我有两个不同的优先级队列,它们使用不同的比较器。我想要一个带有 bool 值的 if/else 来确定哪个 p.q.将被设置为一个变量(例如,“pq”);
比如我有
priority_queue<test, vector<test>, CompareTest1> pq;
我如何将它放在 if/else 中,这样如果一个 bool 值被标记,pq 将被设置为...
priority_queue<test, vector<test>, CompareTest2> pq;
使用不同的比较器。谢谢。
最佳答案
这无法完成,因为如果您提供不同的模板参数,则两个模板是不同的类型。我的建议是传递一个具有 bool 值的比较仿函数,该 bool 值由 if 语句中的内容决定。例如:
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) {
}
};
关于C++ 在 else/if 中声明一个优先级队列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20061613/