gpt4 book ai didi

c++ - std::priority_queue:自定义排序而不定义比较器类

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:12:07 25 4
gpt4 key购买 nike

我想要一个自定义排序的优先级队列,但我很懒惰,不想定义一个实现 operator() 的比较器类。

我真的很想编译这样的东西:

std::priority_queue<int, std::vector<int>, 
boost::bind(some_function, _1, _2, obj1, obj2)> queue;

其中 some_function 是一个带有四个参数的 bool 返回函数,第一个和第二个是队列的整数,最后两个是计算排序所需的一些对象(const 引用)。

(error: ‘boost::bind’ cannot appear in a constant-expression)

但这不能编译。甚至更简单

std::priority_queue<int, std::vector<int>, &compare> queue;

不会编译,比较是一个返回 bool 值的二进制函数。

(error: type/value mismatch at argument 3 in template parameter list for ‘template class std::priority_queue’; expected a type, got ‘compare’)

有什么建议吗?

最佳答案

这可行:

std::priority_queue<int, std::vector<int>, 
boost::function<bool(int,int)> >

然后将绑定(bind)表达式传递给队列的构造函数。

附言您会收到这些编译错误,因为您将运行时计算的表达式放在需要类型名或常量表达式的位置。

关于c++ - std::priority_queue:自定义排序而不定义比较器类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4186651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com