std::string::erase 正在删除迭代器后面的所有字符,而不仅仅是迭代器

std::string::erase is deleting all the characters following the iterator instead of only the iterator

我正在尝试从字符串中删除一个字符。我试过下面的代码:

size_t it = s.find(char(i+97)); //dont mind the i, it is just the int in a for loop.
s.erase(it);

这是一个测试用例:
输入:“cccaabababaccbc”
输出:“ccc”
关于为什么会发生这种情况的任何想法?

当你传入一个索引时,要擦除,它会一直到字符串的末尾。将其更改为 s.erase(it,1)。第二个参数表示要删除的字符数。

我建议不要将变量命名为 it,该名称通常用于迭代器类型。

如果你传递一个迭代器来擦除,那么只有那个字符被删除。

这是文档:https://www.cplusplus.com/reference/string/string/erase/