C++ std::string find_first_of() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::string

Definition

In C++, std::string::find_first_of() scans left to right for the first character that appears in a set of characters.

If none match, the result is npos. Include <string>.

This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.

Syntax

Pass the set of characters to look for.

C++
auto p = s.find_first_of("aeiou");
auto q = s.find_first_of(" ,.", 3);
CallWhat it does
s.find_first_of(set)First index whose character is in set.
s.find_first_of(set, pos)Same, starting the search at pos.

The argument is a set of individual characters, not a substring to match as a whole. For substrings use find.

Library declarations
C++
size_t find_first_of(const string& str, size_t pos = 0) const;
size_t find_first_of(char ch, size_t pos = 0) const;

Parameters

ParameterDescription
str / setCharacters that count as a match.
posWhere to start scanning.

Return Value

Return ValueDescription
Index or nposPosition of the first matching character.

C++ Examples

Example 1 — Basic find_first_of

Find the first vowel.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "sky";
    std::size_t p = s.find_first_of("aeiou");
    if (p == std::string::npos) {
        std::cout << "no vowel\\n";
    } else {
        std::cout << "at " << p << '\\n';
    }
    return 0;
}

How It Works

  1. Characters checked against the vowel set.
  2. None of s,k,y are vowels.
  3. Result is npos.

Example 2 — Find Separator

Find the first space or comma.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "one,two";
    std::size_t p = s.find_first_of(" ,");
    std::cout << p << ' ' << s[p] << '\\n';
    return 0;
}

How It Works

  1. Scan finds ',' at index 3.
  2. That character is in the set.
  3. Useful for simple tokenization starts.

Example 3 — Start at pos

Skip the first match by starting later.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "a-b-c";
    std::size_t p1 = s.find_first_of('-');
    std::size_t p2 = s.find_first_of('-', p1 + 1);
    std::cout << p1 << ' ' << p2 << '\\n';
    return 0;
}

How It Works

  1. First dash is at 1.
  2. Second search starts after it.
  3. Next dash is at 3.

Frequently Asked Questions

It finds the first character that matches any character in a given set.
No. Each character in the argument is a candidate match.
Returns npos.
Use find(), not find_first_of().
find_first_not_of finds the first character not in the set.

Next: find_last_not_of()

Keep going through the string functions. Find the last character that is not in a given set.

find_last_not_of() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful