我有一個類Node
,除了存儲數據,有一個指針指向其父Node
對象。我在priority_queue
中存儲了一些節點,並將<
運營商作爲比較。priority_queue指向彼此
class Node {
public:
string name;
Node *parent;
int cost;
};
static bool operator<(const Node& lhs, const Node& rhs) {
return lhs.cost < rhs.cost;
}
priority_queue<Node> queue;
的問題是,父指針似乎搞砸了。我的猜測是,當我從隊列彈出一個Node
,該Nodes
實際上是在內存中向上移動,並使指針指向錯誤Nodes
。這可能嗎?
我試着用指針的priority_queue
代替Node*
(並用new
創建它們),這樣只有指針被重新排序,而不是對象本身。這似乎解決了指針問題,但現在隊列按內存地址排序,而不是Nodes
的成本。
我怎樣才能實現一個priority_queue
具有指向對方的對象?
[如何實施對於C排序方法++優先\具有指針_queue](可能重複http://stackoverflow.com/questions/986021/how-to-implement-sorting-method-for-ac-priority-隊列指針) – WhozCraig