C++でint型の変数に入っている整数をstring型の文字列に変えたい
C++でAtcoder Beginner Contest 075のB問題であるMinesweeperを解いています。
その回答をする上でint型の整数をstring型の文字列に変換してもう一度処理して出力したいのですが、実際に文字列を入力してみるとエラーに阻まれます。
発生している問題・エラーメッセージ
terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at __n(which is 5) >=this->size() (which is 1)
該当のソースコード
// Minesweeper #include <bits/stdc++.h> using namespace std; int main () { int L,H,l1,l2,l3,l1c,l2c,l3c; cin >> L >> H; l1 = 0; l2 = 0; l3 = 0; l1c = 0; l2c = 0; l3c = 0; int keta,Lche; keta = 0; Lche = L; while ( Lche>0 ) { Lche /= 10; keta ++; } while ( H>0 ) { l3 = 0; string str1; cin >> str1; for ( int i=0; i<L ; i++ ) { int ri,num; ri = 1; if ( str1.at(i)!='.' ) { num = 1; } else { num = 0; } l3c += num*ri; } int d=1; for ( int j=0; j<keta-1 ; j++) { d *= 10; } l3 = l3c; l3c = l3c + l3c/10 + l3c*10 - ((l3c*10)/(d*10))*(d*10); l2c = l2 + l2/10 + l2*10 - ((l2*10)/(d*10))*(d*10); l1c = l1 + l1/10 + l1*10 - ((l1*10)/(d*10))*(d*10); l2c += l1c + l3c; string line = to_string(l2c); for ( int k=0; k<L ; k++ ) { if ( str1.at(k)=='.' ) { cout << line.at(k); } else { cout << str1.at(k); } } cout << endl; l1 = l2; l2 = l3; H--; } return 0; }
試したこと
無いものをもらってこようとしてエラーを起こしていたのかと予想して文字列の長さを確認してみましたがどうもうまくいきません。
補足情報(FW/ツールのバージョンなど)
MinGWを使っています、Windows7です。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/01 14:44
2018/08/01 14:57