私はC++が初めてです。データをファイルに出力できません。イテレータを使用してマップを出力しています。print メソッドは、キー値である i を受け取り、それに対応するベクトルを出力します。これで、cout<< を使用して通常どおり出力すると問題なく動作しますが、同じ出力をファイルに入れようとすると、プログラムがクラッシュします。outfile<< 行でクラッシュしているのは *it であることはわかっています。これをランダムな文字列に置き換えると、ファイルに正常に出力されるためです。また、そのメソッドをプログラムのメイン関数に直接転送して同じエラーを取得できるため、print メソッドのパラメーターが問題を引き起こしていないこともわかっています。これを修正する方法について何か助けていただければ幸いです。ありがとうございます! エラーが発生している印刷方法は次のとおりです。
public: void print(int i, vector<string> in, ostream& outfile) // print method for printing a vector and it's key
{
sort(in.begin(), in.end()); // sort the vector alphabetically first
vector<string>::iterator it;
it= unique(in.begin(), in.end()); // makes sure there are no duplicate strings
in.resize( distance(in.begin(),it) );
for( it = in.begin(); it != in.end(); it++ ) // iterate through it
cout << i << ": "<< *it<<endl; // and print out the key value and each string in the vector
// outfile<< i << ":" << *it<< endl; // prints to file
}