string c++ 详解 erase find

string& erase ( size_t pos = 0, size_t n = npos ); 删除字符串从位置pos(默认为0)的npos个字符。

iterator erase ( iterator position ); 删除字符串从位置position开始之后的所有字符

iterator erase ( iterator first, iterator last ); 删除字符串从迭代位置first到last之间的所有字符

删除字符范围后的字符按顺序顺接到之前未被删除的字符之后。

Erase characters from string
Erases a part of the string content, shortening the length of the string.

The characters affected depend on the member function version used:

string& erase ( size_t pos = 0, size_t n = npos );
Erases a sequence of n characters starting at positionpos. Notice that both parameters are optional: with only one argument, the function deletes everything from positionpos forwards, and with no arguments, the function deletes the entire string, like member clear .
iterator erase ( iterator position );
Erases the character referred by the iterator position. Only one character is affected.
iterator erase ( iterator first, iterator last );
Erases all the characters between first andlast.

 

 

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


 

 

 

string::find

size_t find ( const string& str, size_t pos = 0 ) const;
从位置pos开始查找字符串str, 如果查到返回位置地址的值
size_t find ( const char* s, size_t pos, size_t n ) const;
从原字符串的位置pos开始查找 目标字符数组s的首地址开始的连续n个字符,找到返回首地址,找不到返回-1
size_t find ( const char* s, size_t pos = 0 ) const;
从地址pos开始查找字符数组s, 找到返回首地址
size_t find ( char c, size_t pos = 0 ) const;
从地址pos开始查找字符c,找到返回首地址
 
Find content in string

Searches the string for the content specified in eitherstr,s or c, and returns the position of the first occurrence in the string.

When pos is specified the search only includes characters on or after positionpos, ignoring any possible occurrences in previous locations.

Notice that unlike member
find_first_of, whenever more than one character is being searched for, it is not enough that only one of these characters match, but the entire sequence of characters to find must be matched.

 

 

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// string::find
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str ("There are two needles in this haystack with needles.");
  string str2 ("needle");
  size_t found;

  // different member versions of find in the same order as above:
  found=str.find(str2);
  if (found!=string::npos)
    cout << "first 'needle' found at: " << int(found) << endl;

//从原字符串str的14+1 =15的位置之后开始查找,查找目标字串needles are small的首6个字符,找到位置即第44个位置。返回found = 44
  found=str.find("needles are small",found+1,6);
  if (found!=string::npos)
    cout << "second 'needle' found at: " << int(found) << endl;

  found=str.find("haystack");
  if (found!=string::npos)
    cout << "'haystack' also found at: " << int(found) << endl;

  found=str.find('.');
  if (found!=string::npos)
    cout << "Period found at: " << int(found) << endl;

  // let's replace the first needle:
  str.replace(str.find(str2),str2.length(),"preposition");
  cout << str << endl;

  return 0;
}
 
Notice how parameter pos can be used to search for a second instance of the same search string. Output:

      
      
first 'needle' found at: 14
second 'needle' found at: 44
'haystack' also found at: 30
Period found at: 51
There are two prepositions in this haystack with needles.



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值