使用自定义比较器函数在类中定义优先级队列
Defining priority queue inside a class with a custom comparator function
我正在尝试使用自定义比较器定义优先级队列,如下所示:
typedef bool (*comp)(int,int);
bool compare(int exp1,int exp2){
return (exp1 > exp2);
}
class test{
public:
priority_queue<int,vector<int>,comp> test_pq(compare); // Gives compilation error
};
int main ()
{
priority_queue<int,vector<int>,comp> pq(compare); // Compiles perfectly
return 0;
}
这是显示
的编译错误test.cpp:18:47: error: ‘compare’ is not a type
priority_queue<int,vector<int>,comp> test_pq(compare);
^
我还尝试在测试类中声明另一个比较函数,但没有效果。为什么优先级队列在主要功能编译而一个类内没有?为comparator定义一个专用类是这里唯一的工作吗?谢谢你。
你在test
类中的代码试图声明一个签名不正确的方法test_pq
。
要定义成员变量,可以在初始化时使用花括号(c++ 11要求):
class test{
public:
priority_queue<int,vector<int>,comp> test_pq{compare};
};
要在c++ 11之前实现相同的功能,您需要为test
类编写自定义构造函数:
class test
{
public:
test()
: test_pq(compare)
{
// Constructor code here
}
private:
priority_queue<int,vector<int>,comp> test_pq;
};
相关文章:
- 带自定义比较器的最小优先级队列
- 优先级队列自定义比较器
- Cython中带有自定义比较器的优先级队列
- 具有自定义对象的C 优先级队列
- 用户定义与自动模板扣除指南的优先级
- 我正在声明一个自定义优先级队列,包括 pair<pair<int,int>int >,如何清除它?
- C++ 具有自定义比较函数的优先级队列在 push() 上行为不正确
- 通过引用传递自定义优先级队列
- 尝试使用自定义类的结构中的变量创建该类的优先级队列
- 使用自定义比较器声明C++优先级队列的问题
- 优先级队列自定义比较器的返回值表示什么
- 如何在 c++ 中定义具有四个值的优先级队列
- 访问类的私有成员的自定义优先级队列比较器
- C++类中具有自定义比较功能的优先级队列
- 如何在优先级队列中使用自定义类
- 用户定义类型的优先级队列
- 第二次推送时,优先级队列出现自定义比较器分段故障
- 为优先级队列的基础容器使用自定义类型的列表
- 使用自定义比较器函数在类中定义优先级队列
- 混淆了定义自定义优先级队列和使用C++中的make_heap等方法初始化堆之间的区别