C++ 标准库 std::remove

参见:https://zh.cppreference.com/w/cpp/algorithm/remove

std::remove 不会改变输入vector / string 的长度。其过程,相当于去除指定的字符(以string为例),剩余字符往前靠。后面的和原始字符保持一致。详见示例程序结果

#include <algorithm>
#include <string>
#include <iostream>
#include <cctype>
 
int main()
{
    std::string str1 = "Text with some   spaces 123";
    std::remove(str1.begin(), str1.end(), ' ');
    std::cout << str1 << '\n';
}

输出结果如下:

Textwithsomespaceses 123

请注意最后6位。

猜你喜欢

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