// string::erase
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("This is an example phrase.");
string::iterator it;
// erase used in the same order as described above:
//删除从位置10(从0开始算的,T为第一个位置0)开始的e之后的8个字符"example "
str.erase (10,8);
cout << str << endl; // "This is an phrase."
//删除从迭代器开始str.begin()所指的为位置0, +9表示后移9个位置,即指向第十个位置的地址。然后删除该位置的字符 即删除字符n
it=str.b egin()+9;
str.erase (it);
cout << str << endl; // "This is a phrase."
//删除从第五个位置开始即i,到倒数第7个字符即空格之间的字符
str.erase (str.begin()+5, str.end()-7);
cout << str << endl; // "This phrase."
return 0;
}
|