[英]C++ overloading > for priority_queue (min heap): yields “invalid operands to binary expression ('const Node' and 'const Node')” on push() for heap
I am trying to overload the > operator so I can do this for a priority queue: 我正在尝试重载>运算符,因此可以对优先级队列执行此操作:
priority_queue<Node, vector<Node>, greater<Node> > unexplored_nodes;
I would like to use it as a min-heap. 我想将其用作最小堆。
This is the code for the Node struct (I know, it's not best to have a struct with public variables, but I just wanted something quick and dirty): 这是Node结构的代码(我知道,最好不要使用带有公共变量的结构,但我只想快速又脏一些):
struct Node {
string repr;
float d;
vector<pair<Node, float> > neighbors;
Node(string repr) {
this->repr = repr;
this->d = FLT_MAX;
}
// **All three of these forms yield errors**
// Compiles without push() call below, but yields "invalids operands to binary expression" if I do have the push() call
//bool operator()(const Node* lhs, const Node* rhs) const {
// return lhs->d > rhs->d;
//}
// Compiles without push() call below, but yields "invalids operands to binary expression" if I have do the push() call
bool operator>(const Node &rhs) {
return d > rhs.d;
}
// Error regardless of priority queue below: overloaded 'operator>' must be a binary operator (has 3 parameters)
//bool operator>(const Node &lhs, const Node &rhs) {
// return lhs.d > rhs.d;
//}
};
void foo(const vector<Node> &list) {
priority_queue<Node, vector<Node>, greater<Node> > q;
q.push(list[0]); // this causes the 1st & 2nd overload attempt in the struct to have the "invalid operands" error
}
This is how I am compiling it: 这就是我的编译方式:
clang++ -std=c++11 -stdlib=libc++ thefile.cc
In all 3 of the ways I tried, some error appeared (commented in the code). 在我尝试的所有3种方法中,都出现了一些错误(在代码中进行了注释)。 I had looked at Creating Min Heap from STL Priority Queue for ways to implement it (I tried the one by larsmans), but they haven't worked.
我曾从STL Priority Queue中创建Min Heap,以了解实现该方法的方法(我由larsmans尝试过),但是它们没有用。
If anyone can help, I would appreciate it. 如果有人可以提供帮助,我将不胜感激。 (Also if you have a better way to use the priority queue as a min heap that avoids operator overloading, that would be good too... this is the main purpose.)
(另外,如果您有更好的方式将优先级队列用作最小堆,以避免操作员重载,那也将是个好主意……这是主要目的。)
You must mark the operator as const
: 您必须将运算符标记为
const
:
bool operator> (const Node &rhs) const {
return d > rhs.d;
}
EDIT 编辑
If you don't want to overload operators, you can provide your own comparator to the queue: 如果不想重载运算符,则可以为队列提供自己的比较器:
struct Comparator
{
bool operator() (const Node &lhs, const Node &rhs) const
{
return lhs.d > rhs.d;
}
};
void foo(const vector<Node> &list) {
priority_queue<Node, vector<Node>, Comparator> q;
q.push(list[0]);
}
EDIT 2 编辑2
Here's how you could use a custom map: 这是使用自定义地图的方法:
struct Comparator
{
typedef std::map<Node, float> Map;
explicit Comparator(const Map &map) : map(&map) {}
bool operator() (const Node &lhs, const Node &rhs) const
{
return map->find(lhs)->second > map->find(rhs)->second;
}
private:
const Map *map;
};
void foo(const vector<Node> &list, const Comparator::Map &map) {
priority_queue<Node, vector<Node>, Comparator> q(Comparator(map));
q.push(list[0]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.