C++标准库删除字符串中指定字符,比如空格

参见: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

猜你喜欢

转载自www.cnblogs.com/alexYuin/p/11546159.html