#include <iostream>
#include <string>
#include <map>
#include <set>
using namespace std;
int main()
{
string str1 = "example.";
for(string::iterator it = str1.begin();it != str1.end(); ++ it)
{
if(*it == '.')
//下面这个erase必须使用返回值,也就是让it指向erase的返回值
it = str1.erase(it);
//如果删除的是string最后一个元素,那么删除后上面的it立刻等于str1.end(),那么如果此时
//不跳出循环,让++it运行,势必出错,所以必须加下面的语句。
if(it == str1.end())
break;
}
cout << str1 << endl;
return 0;
}
注意:顺序容器用erase删除一个元素,必须记录erase函数的返回值。不然it将会指向什么地方根本无从得知。
有1个月没编程了,这个破问题竟然困扰了自己一下午。加油!!!坚持才是王道。