因此,我试图从字符串中删除空格,但是如果输入例如“ hello world”,则仅返回“ hello”,而不返回“ helloworld”。我不确定为什么要这样做。
string removeSpaces(string str)
{
str.erase(remove(str.begin(), str.end(), ' '), str.end());
return str;
}
int main()
{
std::string input;
std::cout << "Enter word: ";
std::cin >> input;
input = removeSpaces(input);
std::cout << input;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题不在于功能(据我所知)。它与您读取输入的方式相同。
std::cin >> input将读取直到空白。因此input将是“你好”。
阅读整行使用
std::getline(std::cin, input);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
74 次 |
| 最近记录: |