Translations of this page?:

find_first_of

Syntax:

    #include <string>
    size_type find_first_of( const string &str, size_type index = 0 ) const;
    size_type find_first_of( const charT* str, size_type index = 0 ) const;
    size_type find_first_of( const charT* str, size_type index, size_type num ) const;
    size_type find_first_of( charT ch, size_type index = 0 ) const;

find_first_of()関数は、いずれかの動作をします。

  • 現在の文字列から”str”に含まれる文字にマッチした最初の文字の位置を返します。検索開始位置を”index”で与えることができ、見つからなかったら”string::npos”を返します。
  • 現在の文字列の”index”番目から”num”文字に対して、文字列”str”の文字を検索します。最初にマッチした位置を返し、見つからなかったら”string::npos”を返します。
  • 文字'ch'が現在の文字列に対して、最初にマッチしなかった位置を返します。検索開始位置を”index”で与えることができ、見つからなかったら”string::npos”を返します。

例えば、次のコードは find_first_of を使い、母音をアスタリスクに置換しています。

  string str = "In this house, we obey the laws of thermodynamics!";
  string::size_type found = str.find_first_of("aeiouAEIOU");
 
  while( found != string::npos ) {
    str[found] = '*';
    found = str.find_first_of("aeiouAEIUO",found+1);
  }
 
  cout << str << '\n';  // displays "*n th*s h**s*, w* *b*y th* l*ws *f th*rm*dyn*m*cs!"

Related Topics: find, find_first_not_of, find_last_not_of, find_last_of, rfind

 
• • • SitemapRecent changesRSScc