私はVS2015 C++を使用しています。私はファイルを読み込み、whileループを使ってベクトルに行単位で入力しようとしました。デバッグアサーション失敗したベクトルの添え字が範囲外C++
Debug Assertion Failed!
Program: C:\Windows\SYSTEM32\MSVCP140D.dll
File: c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector
Line: 1234
Expression: vector subscript out of range
For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.
私のコードは次のとおりです。:
53.74
54.09
53.5
53.72
53.43
:
int main() {
std::ifstream inf("walmart2.txt");
std::vector<std::string> blah;
int j = 0;
if (!inf) {
std::cerr<< "Uh oh, walmart2.txt could not be opened for reading!" << std::endl;
exit(1);
}
while (inf)
{
std::string strInput;
inf >> strInput;
blah[j] = strInput;
j = j + 1;
}
std::cout << blah.size() << '\n';
return 0;
}
ファイル "walmart2.txt" は、次の形式で約1800行です
は、私はこのエラーを取得します
私は何が起こっているかについて完全にはわかっていません。どんな助けもありがとうございます。
'何とか[J] = strInput;' => '何とか.push_back(strInput); ' –
あなたの' blah'のように)デフォルトで構築されたベクトルは*空です*。つまり、それらの中のインデックス付け(インデックス '0'でも)は範囲外になります。 –