2016-05-19 4 views
-1

ファイルからmultimap<int,string>を読み込む必要がありますが、その方法を理解できません。私はそれを実行するとマルチマップを読む

ifstream in ("words.txt"); 
multimap<int, string> words; 
int count = 0; 
while (!in.eof()) { 
     getline(in, words[count]); 
     count++; 
} 

は私がin >> words[count]てみましたerror: no match for ‘operator[]’ (operand types are ‘std::multimap<int, std::__cxx11::basic_string<char> >’ and ‘int’) getline(in, words[count]); このエラーを取得し、それはあまりにも動作しません。私はこれをどのように修正すべきですか?

+0

あなたのエラーには直接関係しませんが、あなたに噛まれる前に:[eof()をループ条件として使用しないでください](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside -a-loop-condition-considered-wrong)を指定します。 – Quentin

+0

a)使用しないでください!(!in.eof)、googleそれはあなたがstackoverflowで答えを見つけるb)マルチマップ[doesntはその演算子を持っているようです](http://en.cppreference.com/w/cpp/container/multimap ) – Borgleader

答えて

1

multimapにはoperator[]がありません。

multimapの方法、またはemplaceの方法を使用することができます.C++ 11を使用する場合は、メソッドを使用します。

emplaceのマニュアルと例については、hereを参照してください。

0

使用.insert()メソッド:あなたはあなたがのstd ::ベクトルと一back()を使用する場合があります以降のインデックスの各ペアをしたい場合も

cplusplus reference

0

には[]ありません演算子はstd::multimapです。 行を文字列として読み込み、insertを使用する必要があります。