The find() Family

Here are the find() prototypes as provided by C++11:

size_type find (const basic_string& str, size_type pos = 0) const noexcept;size_type find (const charT* s, size_type pos = 0) const;size_type find (const charT* s, size_type pos, size_type n) const;size_type find (charT c, size_type pos = 0) const noexcept;

The first member returns the beginning position of the str substring’s first occurrence in the invoking object, with the search beginning at position pos. If the substring is not found, the method returns npos.

Here’s code for finding the location of the substring "hat" in a longer string:

string longer("That is a funny hat.");string shorter("hat");size_type loc1 = longer.find(shorter);           // sets loc1 to 1size_type ...

Get C++ Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.