<分区>
我想从字符串中删除一个符号“:”。
std::cout << str << " "; //1948: 59 63
std::remove(str.begin(), str.end(), ':');
std::cout << str << " ";//1948 59 633
如果我将范围更改为 [str.negin(), str.begin()+5) 一切正常。
<分区>
我想从字符串中删除一个符号“:”。
std::cout << str << " "; //1948: 59 63
std::remove(str.begin(), str.end(), ':');
std::cout << str << " ";//1948 59 633
如果我将范围更改为 [str.negin(), str.begin()+5) 一切正常。
最佳答案
std::remove
不会从范围中移除元素。无法使用迭代器删除元素。但是,它会将您不想删除的所有元素移到开头,然后返回一个指向这些项目末尾的迭代器。
std::remove
的正确使用方法是这样的:
str.erase(std::remove(str.begin(), str.end(), ':'), str.end());
它会将所有“好”元素移到前面(std::remove
这样做),然后删除剩余的元素(str.erase
这样做)那个)。
关于c++ - 为什么字符串中的最后一个符号在 'std::remove' 之后加倍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59378761/
相关文章:
c++ - 请求从 `double' 到非标量类型的转换和一些其他错误
python - 如何在 python 中合并重叠的字符串?
string - OleVariant 可用于 DLL 过程中的输出参数吗?
java - 实现 Java 的 indexOf 方法(子串搜索)
c++ - ifstream gcount 在 getline 字符串过载时返回 0
c++ - 使用 MS Visual Studio 2010 编译 C++ 程序不依赖于任何外部代码或可再发行组件
c++ - 在 C 和 C++ 中,为什么每个 .h 文件通常都用#ifndef#define #endif 指令包围?