比较c++中对象优先队列的函数
Compare function for an object Priority Queue C++
我需要一个比较函数在我的优先级队列中使用来比较对象。对象需要按对象的比例排序。由于某些原因,RatioCompare函数无法工作/编译。
编译器给出如下错误:
成员函数
bool RatioCompare::operator()(const Item&, const Item&) const
:joulethief.cpp:58
: error: passing ' const Item '作为double Item::getRatio()
的' this '参数丢弃了限定符joulethief.cpp:59: error: passing ' const Item '作为double Item::getRatio()
的this
参数丢弃了限定符
你们能看一下吗?
struct RatioCompare
{
bool operator()(const Item &i1, const Item &i2) const
{
double i1value= i1.getRatio();
double i2value= i2.getRatio();
return i1value < i2value;
}
};
下面是我声明它的代码,然后测试它是否在main中工作…
priority_queue<Item, vector<Item>, RatioCompare > pq;
for(int i=0; i<n; i++)
{
pq.push(tosteal[i]);
}
while(!pq.empty())
{
Item consider= pq.top();
cout<< "Name: "<< consider.getName()<< "Ratio "<< consider.getRatio()<<endl;
pq.pop();
}
我在程序中也包含了队列和向量库。
成员函数Item::getRatio()
需要标记为const
,否则编译器认为该方法可以改变Item
实例,从而阻止您在将所述Item
实例传递为const_reference
时使用它(就像您在RatioCompare
的operator()
中所做的那样)。
将Item::getRatio
的定义改为:
class Item {
public:
// ...
double getRatio() const; // marked as const, does not alter Item instances
};
相关文章:
- 尝试将lambda函数放在队列中时出现一般分配器错误(可能是与unique_ptr有关的错误)
- 使用"Task"函数指针队列定义作业管理器
- 复制构造函数优先于移动构造函数
- 优先队列的自定义比较器
- 优先队列中比较函数的工作
- 比较优先队列中的参数
- 比较优先队列的类
- 优先队列中的结构比较
- 如何在我的优先队列中访问10个最大数字
- 可变参数构造函数优先于用户提供的移动构造函数,除非默认
- 优先队列未正确比较C
- 指针指向结构的指针(优先队列)
- 优先队列,超载操作少
- 为什么构造函数优先于用户定义的运算符
- C++:为什么成员函数优先于全局函数
- <vector> 使用 C++ 中的函数返回队列
- 如何在c++中创建一个函数指针队列
- 优先队列排序[c++]
- 我如何在优先队列中找到值
- 比较c++中对象优先队列的函数