您的位置:首页 > 编程语言 > C语言/C++

string c++ 详解 erase find .

2015-09-23 19:33 399 查看

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

// 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;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: