string c++ 详解 erase find .

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.begin()+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;
}
posted @ 2015-09-23 19:33  小松鼠。  阅读(385)  评论(0编辑  收藏  举报