Translations of this page?:

find_first_of

Sintaxe:

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

A função find_first_of:

  • devolve a posição do primeiro caracter da string actual que corresponde a um caracter de str, começando a procurar em index, devolvendo string::npos se nada for encontrado,
  • procura na string actual, começando em index, por qualquer dos primeiros num caracteres em str, devolvendo a posição na string actual do primeiro caracter encontrado, ou string::npos se nenhum corresponde,
  • ou devolve a posição da primeira ocorrência de ch na string actual, começando a procurar em index, devolvendo string::npos se nada for encontrado.

Por exemplo, o código seguinte usa find_first_of para substituir todas as vogais numa string com asteriscos:

  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';  // mostra "*n th*s h**s*, w* *b*y th* l*ws *f th*rm*dyn*m*cs!"

Tópicos Relacionados: find, find_first_not_of, find_last_not_of, find_last_of, rfind

 
• • • SitemapRecent changesRSScc