参见:https://zh.cppreference.com/w/cpp/algorithm/remove
使用 erase 和 remove 配合。
#include <algorithm>
#include <string>
#include <iostream>
#include <cctype>
int main()
{
std::string str1 = "Text with some spaces";
str1.erase(std::remove(str1.begin(), str1.end(), ' '),
str1.end());
std::cout << str1 << '\n';
}
输出结果是:
Textwithsomespaces