find_first_of

find_first_of (집합 원소 중 첫 등장 찾기)

첫 범위에서 두 번째 범위(집합)에 속한 원소 중 가장 먼저 등장하는 것의 위치를 찾는 알고리즘이에요. <algorithm> 헤더에 있어요.

출처: cppreference

본문

find_first_of[first1, last1) 안에서 [first2, last2)속한 원소가 처음 등장하는 위치를 찾아요.

template< class InputIt, class ForwardIt >
InputIt find_first_of( InputIt first1, InputIt last1,
                       ForwardIt first2, ForwardIt last2 );   // (1)

비교기 p를 받는 버전도 있어요.

template< class InputIt, class ForwardIt, class BinaryPred >
InputIt find_first_of( InputIt first1, InputIt last1,
                       ForwardIt first2, ForwardIt last2,
                       BinaryPred p );   // (2)
    1. operator==로, 2) 술어 p로 비교해요.
  • 반환 값: [first2, last2)에 속한 원소 중 첫 번째로 등장하는 원소를 가리키는 반복자. 없으면 last1.
std::string s = "Hello World";
std::string vowels = "aeiou";
auto it = std::find_first_of(s.begin(), s.end(),
                             vowels.begin(), vowels.end());
// 'e'의 위치

"금지 문자/찾고 싶은 집합 중 먼저 나오는 것"을 찾을 때 유용해요. find가 단일 값 탐색이라면, find_first_of는 집합 탐색이에요.

더 알아보기 (Learn more)

cppreference