2017-05-24 5 views
-1

私は、2つのActorオブジェクト間の最短経路を見つけるためにdjikstraのアルゴリズムを実行したいActorオブジェクトとMovieオブジェクトの2つのグラフを設計しています。ペアを保持する最小のヒープを作成する<int、custom class>

私はすでにグラフをデザインしてビルドしています。興味のある方はここに私のデザインです...

actor.h:

/* (Vertex) Object Class to represent actors */ 
class ActorNode { 
     friend class Movie; 
     friend class ActorGraph; 
    public: 
     /*Member Variables*/ 
     std::string name; 
     std::set<Movie*> movies; 
     /*Constructor*/ 
     ActorNode(std::string name) : name(name) {} 
     /*Destructor*/ 
     ~ActorNode(); 

     /*Getters and Setters*/ 
     std::string getName(); 
     void setName(std::string actor); 

     /*Member Functions*/ 

}; 

movie.h:

class Movie { 
    public: 
     friend class ActorNode; 
     friend class ActorGraph; 

     std::string name; 
     int year; 
     int weight; 
     std::set<ActorNode*> cast; 
     /*Constructor*/ 
     Movie(std::string name, int year) : name(name), year(year), weight(1) {} 
     /*Destructor*/ 
     ~Movie(); 
     /*Getters and Setters*/ 
     std::string getMovie(); 
     void setMovie(std::string movie); 

     int getYear(); 
     void setYear(int yr); 

     int getWeight(); 
     void setWeight(int wt); 

     /*Member Functions*/ 
}; 

ActorGraph.h:

class ActorGraph { 
     public: 
     unordered_map<std::string,ActorNode*> actorMap; 
     unordered_map<std::string,Movie*> movieMap; 

     ActorGraph(void); 

     bool loadFromFile(const char* in_filename, bool use_weighted_edges); 

    }; 

機能loadFromFileはファイルから読み込みます。ファイルの行は、それぞれの行がその年に並んで出演した俳優と映画です。 テキストファイルからより簡単にグラフを作成することができます。

とにかく私の質問は、私のデータ構造でdjikstraを実装しようとしていることです。

intが、ソースからの距離を表すことになるActorNode *のペアの優先度キューを使用したいとします。優先度のキューは、デフォルトではmax_heapですが、最小のヒープにしたいと考えています。これは、これが表示されているいくつかの他のトピックで説明されてい...

std::priority_queue<int, std::vector<int>, std::greater<int> > my_min_heap; 

ので、私は

std::priority_queue<std::pair<int,ActorNode*>,std::vector<std::pair<int,ActorNode*>,std::greater<int>> min_heap; 

...私の目的のために、この例に従うことをしようとしたが、それはアールの引数が2と3と言います無効。 どのようにして優先キューを作成する方法はありますか?ありがとうございました!

UPDATE

OKので、私は...

/* comparison class */ 
class pairCompare{ 
    public: 
     typedef std::pair<int, ActorNode*> p; 

     struct compare{ 
      bool operator()(const p& a, const p& b) { 
       if (a.first > b.first) return true; 
       else return false; 
      } 
     }; 

    private: 
     std::priority_queue<p,std::vector<std::pair<int,ActorNode*>>,pairCompare> pq; }; 

が、現在は不完全な型のエラーを取得し、私の比較クラスを書かれている...

In file included from /usr/include/c++/5/queue:64:0, 
       from movie.h:6, 
       from actor.h:8, 
       from ActorGraph.h:14, 
       from ActorGraph.cpp:15: 
/usr/include/c++/5/bits/stl_queue.h: In instantiation of ‘class std::priority_queue<std::pair<int, ActorNode*>, std::vector<std::pair<int, ActorNode*> >, pairCompare>’: 
ActorGraph.h:61:84: required from here 
/usr/include/c++/5/bits/stl_queue.h:391:18: error: ‘std::priority_queue<_Tp, _Sequence, _Compare>::comp’ has incomplete type 
     _Compare comp; 

私はこれを調査し、私が見つけた解決策は、私のActorNodeクラスを比較クラス内に宣言しておくべきですが、エラーを修正しませんでした。比較クラスを正しく作成しましたか? minヒープの優先度キューを作成しようとしています。

その他の質問:私はこの比較クラスが動作すると仮定します...私は、メンバー変数、pqを使用してcomparePairsオブジェクトを作成しますか?たぶん、compareクラスを作成するほうが、それを使用するpqを分離する方が簡単でしょう。実際にcomparePairsオブジェクトを作成するのはちょっと変わったようですが、単に優先度キューを作成し、comparePairsを使用して比較するための第3引数として使用します。この行に

+0

これはどのように研究の欠如を示していますか?別のスタックオーバーフローポストから何かをペーストしました。私はそれに合わせてそれに適応しようとしています。正当な理由なしにdownvotingするだけで罰金を科せられます。 – KoalaIsDead

+0

'std :: greater 'は 'std :: pair 'では動作しません。独自の比較関数を作成する必要があります。 –

+1

また、あなたの質問が長すぎて不必要なコードがたくさん含まれていたため、最初のdownvoteがあったようです。問題が何であるかはすぐに分かりませんでした。質問には、必要な最小限の情報が含まれていると、理解して答えやすくなります。 –

答えて

0

、第二引数、

std::vector<std::pair<int,ActorNode*>

std::priority_queue<std::pair<int,ActorNode*>,std::vector<std::pair<int,ActorNode*>,std::greater<int>> min_heap; 

。あなたは、角かっこを閉じないでください。これを試してみてください:私はDjikstraのか*を行うために使用されるサイドノート、オン

std::priority_queue<std::pair<int,ActorNode*>,std::vector<std::pair<int,ActorNode*>>,std::greater<int>> min_heap; 

が、私は std::mapstd::priority_queueよりずっといい行われていることが分かりました。私の側で実装の問題だったかもしれません。パフォーマンスが懸念される場合は std::mapにチェックしてください。

+0

さて、あなたは私の誤りについて絶対に正しいものでした。有難うございます!私はまだ自分の比較クラスを作成する必要があると私は信じています。引数のエラーは修正されましたが、コンパイラは '/usr/include/c++/5/bits/predefined_ops.h:123:46:error:呼び出しに一致しません'(std :: greater )(std: :pair &、std :: pair &) 'のようになります。私は比較クラスを作ることに取り掛かるでしょう!みんな、ありがとう! – KoalaIsDead

+0

@ KoalaIsDead:あなたの問題を解決した場合、答えを正しいものとしてマークしてください。 – nakiya

関連する問題