【发布时间】:2016-06-02 22:54:29
【问题描述】:
考虑以下程序:
#include <iostream>
#include <string>
#include <algorithm> // std::remove
int main ()
{
std::string str ("This is an example sentence blah");
std::remove(str.begin(), str.end(), '\t');
std::cout << str ;
return 0;
}
(is,an) 和 (an,example) 之间有一个制表符,其他是空格。
我预计输出是:
这是一个例句等等
相反,我得到以下输出:
This is an example sentence blahah
额外的“ah”是否存在缓冲区溢出?
更多: 经过几次运行后,我注意到末尾的尾随字母数量与被删除的标签数量成正比。在这种情况下,字符串中有 2 个制表符,因此有 2 个额外的尾随字符。我可以通过使用擦除来处理它,但是为什么会发生呢?我不确定这是设计缺陷还是我遗漏了什么?
【问题讨论】:
-
你试过阅读
std::remove的文档吗? -
改变你的期望。
标签: c++