C ++在else / if中声明一个优先级队列? [英] C++ declare a priority queue in an else/if?

查看:236
本文介绍了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) {
    }
};

这篇关于C ++在else / if中声明一个优先级队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆