简体   繁体   English

C ++重载>对于priority_queue(最小堆):在push()上为堆生成“对二进制表达式无效的操作数('const Node'和'const Node')”

[英]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.

相关问题 使用自定义比较器(实现最小堆)实例化 int 对的 priority_queue 时 C++ 中的类型错误 - Type error in C++ when instantiating a priority_queue of int pairs with a custom comparator (to implement a min heap) C ++)无效的操作数到二进制表达式Error with Priority Queue - C++ ) Invalid operands to binary expression Error with Priority Queue 为什么 const_casting a heap.top() of priority_queue 有未定义的行为? - Why does const_casting a heap.top() of priority_queue have undefined behavior? 二进制表达式的无效操作数(“ int_node”和const“ int_node”) - invalid operands to binary expression ('int_node' and const 'int_node') 将元素推入优先级队列时,二进制表达式的操作数无效 - invalid operands to binary expression when push element in a priority queue C++ / priority_queue / 表达式:无效的比较器 - C++ / priority_queue / Expression: invalid comparator 使用std :: priority_queue推送最小堆是瓶颈 - Pushing in min heap with std::priority_queue is the bottleneck std :: priority_queue的最小堆是我的瓶颈 - Min heap with std::priority_queue is my bottleneck priority_queue :: emplace调用push_heap吗? - priority_queue::emplace calls push_heap? std :: priority_queue与迭代器到std :: map的元素 - 无效堆 - std::priority_queue with iterators to elements of std::map - invalid heap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM