public member function

<string>

std::basic_string::find_first_of

string (1)
size_type find_first_of (const basic_string& str, size_type pos = 0) const;
c-string (2)
size_type find_first_of (const charT* s, size_type pos = 0) const;
buffer (3)
size_type find_first_of (const charT* s, size_type pos, size_type n) const;
character (4)
size_type find_first_of (charT c, size_type pos = 0) const;
string (1)
size_type find_first_of (const basic_string& str, size_type pos = 0) const noexcept;
c-string (2)
size_type find_first_of (const charT* s, size_type pos = 0) const;
buffer (3)
size_type find_first_of (const charT* s, size_type pos, size_type n) const;
character (4)
size_type find_first_of (charT c, size_type pos = 0) const noexcept;

Find character in string

Searches the basic_string for the first character that matches any of the characters specified in its arguments.

When pos is specified, the search only includes characters at or after position pos, ignoring any possible occurrences before pos.

Notice that it is enough for one single character of the sequence to match (not all of them). See basic_string::find for a function that matches entire sequences.

The function uses traits_type::eq to determine character equivalences.


Parameters

str
Another basic_string with the characters to search for.
pos
Position of the first character in the string to be considered in the search.
If this is greater than the string length, the function never finds matches.
Note: The first character is denoted by a value of 0 (not 1): A value of 0 means that the entire string is searched.
s
Pointer to an array of characters.
If argument n is specified (3), the first n characters in the array are searched for.
Otherwise (2), a null-terminated sequence is expected: the length of the sequence with the characters to match is determined by the first occurrence of a null character.
n
Number of character values to search for.
c
Individual character to be searched for.

charT is basic_string's character type (i.e., its first template parameter).
Member type size_type is an unsigned integral type.

Return Value

The position of the first character that matches.
If no matches are found, the function returns basic_string::npos.

Member type

size_type is an unsigned integral type.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// string::find_first_of
#include <iostream>
#include <string>

int main ()
{
  std::string str ("PLease, replace the vowels in this sentence by asterisks.");
  std::string::size_type found = str.find_first_of("aeiou");
  while (found!=std::string::npos)
  {
    str[found]='*';
    found=str.find_first_of("aeiou",found+1);
  }

  std::cout << str << '\n';

  return 0;
}

Pl**s*, r*pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks.


Complexity

Unspecified, but generally up to linear in length()-pos times the number of characters to match (worst case).

Iterator validity

No changes.

Data races

The object is accessed.

Exception safety

If s does not point to an array long enough, it causes undefined behavior.
Otherwise, the function never throws exceptions (no-throw guarantee).

See also

basic_string::find
Find first occurrence in string (public member function)
basic_string::find_last_of
Find character in string from the end (public member function)
basic_string::find_first_not_of
Find non-matching character in string (public member function)
basic_string::replace
Replace portion of string (public member function)
basic_string::substr
Generate substring (public member function)