天天看點

C++ string::find_first_of

文法: 

size_type find_first_of( const basic_string &str, size_type index = 0 ); 

size_type find_first_of( const char *str, size_type index = 0 ); 

size_type find_first_of( const char *str, size_type index, size_type num ); 

size_type find_first_of( char ch, size_type index = 0 ); 

find_first_of()函數: 

查找在字元串中第一個與str中的某個字元比對的字元,傳回它的位置。搜尋從index開始,如果沒找到就傳回string::npos

查找在字元串中第一個與str中的某個字元比對的字元,傳回它的位置。搜尋從index開始,最多搜尋num個字元。如果沒找到就傳回string::npos, 

查找在字元串中第一個與ch比對的字元,傳回它的位置。搜尋從index開始。

Example

// string::find_first_of
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str ("Replace the vowels in this sentence by asterisks.");
  size_t found;

  found=str.find_first_of("aeiou");
  while (found!=string::npos)
  {
    str[found]='*';
    found=str.find_first_of("aeiou",found+1);
  }

  cout << str << endl;

  return 0;
}
           
R*pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks.
經過測試,成功運作。

注意:
單個字元時單引号,多個字元雙引号。
字元下标從零開始。