0
C++で同じ2つの連続する文字を削除するコードを記述しています。例えば :デモコードはout_of_range例外をスローしていますが、私はなぜか分かりません
- aa -> empty string - aabb -> empty string - abba -> aa -> empty string (as removal of 'bb' makes it 'aa') - abab -> abab (not possible)
#include <iostream>
using namespace std;
int main()
{
int i;
string s;
bool match = true;
getline(cin, s);
while (match) {
match = false;
for (i = 0; i < s.length() - 1; i++) {
if (s.at(i) == s.at(i+1)) {
s.erase(i,2);
match = true;
}
}
}
if (s == "") {
cout << "Empty!";
}
else {
cout << s;
}
return 0;
}
どのような文字列でエラーが発生していますか?エラーの内容は何ですか? –
または単に 'std :: regex_replace(s、std :: regex(")。)、 ""); ' – DimChtz