Chr*_*ian 4 c++ boost stl bind priority-queue
我希望有一个具有自定义排序的优先级队列,但是像我一样懒,我不想定义一个实现operator()的比较器类.
我真的希望编译这样的东西:
std::priority_queue<int, std::vector<int>,
boost::bind(some_function, _1, _2, obj1, obj2)> queue;
Run Code Online (Sandbox Code Playgroud)
其中some_function是一个bool返回函数,它带有四个参数,第一个和第二个是队列的整数,最后两个是计算排序所需的一些对象(const引用).
(错误:'boost :: bind'不能出现在常量表达式中)
但这不编译.甚至更简单
std::priority_queue<int, std::vector<int>, &compare> queue;
Run Code Online (Sandbox Code Playgroud)
不会编译,比较是返回bool的二进制函数.
(错误:模板参数列表中参数3的类型/值不匹配'模板类std :: priority_queue';期望类型,得到'比较')
有什么建议?
Ste*_*e M 11
这可能有效:
std::priority_queue<int, std::vector<int>,
boost::function<bool(int,int)> >
Run Code Online (Sandbox Code Playgroud)
然后将绑定表达式传递给队列的构造函数.
PS你得到那些编译错误,因为你把运行时评估的表达式放在需要typename或constant表达式的地方.