【发布时间】:2014-03-25 17:12:47
【问题描述】:
我已经尝试在 C++ 中实现一个优先级队列大约 5 个小时了。
我不相信我的比较函子正在做它应该做的事情,但对于我的生活,我无法弄清楚为什么。
在我的 Node 类的底部我有一个结构 CompareNode,Node 类有一个函数来返回一个 int 成员变量。
class Node
{
public:
Node();
~Node();
Node(const Node &obj);
int GetX() const { return x; }
int GetY() const { return y; }
int GetG() const { return g; }
int GetH() const { return h; }
int GetF() const { return f; }
int GetTerrainPenalty() const { return terrainPenalty; }
bool GetOpenList() const { return openList; }
bool GetClosedList() const { return closedList; }
Node* GetParentNode() const { return parentNode; }
std::vector<Node*> const GetNeighbours() { return neighbours; }
void SetX(int x) { this->x = x; }
void SetY(int y) { this->y = y; }
void SetG(int g) { this->g = g; }
void SetH(int h) { this->h = h; }
void SetF(int f) { this->f = f; }
void SetTerrainPenalty(int t) { this->terrainPenalty = t; }
void SetOpenList(bool b) { this->openList = b; }
void SetClosedList(bool b) { this->closedList = b; }
void SetParentNode(Node* n) { this->parentNode = n; }
void SetNeighbours(std::vector<Node*> n) { this->neighbours = n; }
void AddNeighbour(Node* n) { neighbours.push_back(n); }
// Manahattan Distance
void CalculateH(Node* end);
void CalculateF();
private:
int x;
int y;
int g;
int h;
int f;
int terrainPenalty;
bool openList;
bool closedList;
Node* parentNode;
std::vector<Node*> neighbours;
};
struct CompareNode
{
bool operator()(const Node* lhs, const Node* rhs) const
{
return lhs->GetF() < rhs->GetF();
}
};
在我的 main.cpp 中,我声明了优先级队列。
std::priority_queue<Node*, std::vector<Node*>, CompareNode> openList;
我收到 Debug Assertion Failed 错误,Invalid Heap。
在调试时,似乎当我调用 openList.top() 时它没有返回正确的节点。
任何想法我做错了什么?
【问题讨论】:
-
能否请您发布您的节点的结构..
-
如果 CompareNode 得到两个 const Node* 不应该是 priority_queue
, CompareNode> 吗? -
没有 priority_queue
, CompareNode> 我无法将指针对象推送到队列中。例如节点* n; openList.push(n); -
为什么你需要 c++ 来完成你的任务?看起来你没有时间真正学习它。如果你不真的学习 c++,你就有麻烦了。改用 python(或 java 或 c#)。
-
@lowtech 很有帮助..
标签: c++ priority-queue