1

コンストラクターからpriority_queueに自分自身を挿入するEdgeと呼ばれるオブジェクトを作成したいと思います。あれは;

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);
}

難しいのは、いつものニワトリと卵の問題です。edges は Edges の priority_queue であるため、Edge が何であるかを知る必要があります。さらに、Edge に対して演算子をless-than オーバーロードする必要があるため、優先キューをインスタンス化する前に演算子を定義する必要がありますが、Edge が定義されていないため定義できません。さまざまな方法で試してみましたが、何も機能しません。もちろん、コンストラクターを呼び出すコードで Edge をプッシュすることもできます。

edges.push(Edge(from,to,cost));

しかし、これを強制する方法が必要なようです。基本的に、これらのオブジェクトは作成時にpriority_queueに入れる必要があると言っているので、それが起こることを保証しましょう.

4

1 に答える 1

3
/* 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;
于 2013-09-11T18:31:52.923 に答える