[英]insert into priority_queue in constructor
我想創建一個稱為Edge的對象,該對象將其自身從其構造函數插入到priority_queue中。 那是;
Class Edge {
int m_from;
int m_to;
int m_cost;
public:
edge(from, to, cost) : m_from(from), m_to(to), m_cost(cost) {
edges.push(this);
}
困難是通常的雞對蛋問題。 edge是Edge的priority_queue,因此它需要知道Edge是什么。 另外,它需要操作符少於Edge的重載,因此需要在我可以實例化優先級隊列之前定義操作符,但由於未定義Edge,因此無法定義它。 我嘗試了很多不同的方法,但是沒有任何效果。 當然,我可以在調用構造函數的代碼中推送Edge
edges.push(Edge(from,to,cost));
但是似乎應該有一種方法可以強制執行此操作。 基本上,我是說這些對象在創建時需要繼續在priority_queue上,所以我們保證會發生這種情況。
/* In .h*/
class Edge {
int m_from;
int m_to;
int m_cost;
static priority_queue<Edge*> edges;
public:
Edge(from, to, cost) : m_from(from), m_to(to), m_cost(cost) {
edges.push(this);
}
}
bool operator < (const Edge* first, const Edge* second) { return first->m_cost < second->m_cost; }
/*In .cpp */
priority_queue<Edge*> Edge::edges;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.