我想刪除字符串中的空格,但是當我運行我的程序並進入「hello world」時,它沒有跟我說話。它崩潰,這表明:爲什麼我無法刪除字符串的空間?
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str;
getline(cin,str);
int space;
while(str.find(' ')>=0) {//something wrong happened in the loop
space = str.find(' ');
str.erase(space,1);
}
cout<<str<<endl;
return 0;
}
我很困惑極了,因爲我不知道爲什麼我的字符串是出於range.So如何解決?提前致謝。
我想知道爲什麼我的程序崩潰而不是如何刪除空間 – fgksgf
'#include':這是一個不 - no ... –
3442
我看,比ks爲您提醒。 – fgksgf