Creating Min Heap from STL Priority Queue

丶灬走出姿态 提交于 2019-12-04 09:46:39

The third template parameter must be a binary functor taking teo Plane*. Your Plane class does not provide that.

You need something of the form

struct CompPlanePtrs
{
  bool operator()(const Plane* lhs, const Plane* rhs) const {
    return lhs->fuel > rhs->fuel ;
  }
};

If you drop the use of pointers (which are overkill for your simple structures), then you can use std::greater from the header functional:

std::priority_queue<Plane, std::vector<Plane>, std::greater<Plane> > pq1;
pq1.push(Plane(0, 0));

Currently, you are feeding Plane as the comparison type. That won't work since the comparison type must be a type of function object, i.e. it must have an operator() that does the comparison. Plane doesn't have such a member (and adding it just for this purpose would be a bad idea).

std::greater has the appropriate method, implemented in terms of your operator>. However, it doesn't work with pointers, because then it uses a pointer comparison (based on memory addresses).

Btw., note that your comparison function can be expressed more succinctly as

bool operator>(const Plane &other)
{
    return fuel > other.fuel;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!