algorithm_find_first_of

algorithm_find_first_of (여러 값 중 처음 찾기)

std::find_first_of는 소스 범위에서 대상 범위에 속한 요소 중 아무것과 일치하는 첫 번째 요소를 찾아요. 여러 후보 값 중 하나를 가장 먼저 찾는 데 써요.

출처: cppreference

본문

std::find_first_of는 소스 범위 [first1, last1)에서 대상 범위 [first2, last2)의 요소 중 아무것과 일치하는 요소를 찾아요. <algorithm> 헤더에 정의되어 있어요.

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

template< class InputIt, class ForwardIt, class BinaryPred >
InputIt find_first_of( InputIt first1, InputIt last1,
                       ForwardIt first2, ForwardIt last2,
                       BinaryPred p );
  • 1번 오버로드 — 요소를 operator==로 비교해요.
  • 2번 오버로드 — 요소를 이진 술어 p로 비교해요.
  • 병렬 실행 정책을 받는 오버로드도 있어요.

반환값 (Return value)

대상 범위의 어떤 요소와도 일치하는 소스 범위의 첫 번째 반복자예요. 일치하는 요소가 없으면 last1을 돌려줘요.

복잡도 (Complexity)

Sstd::distance(first1, last1), Nstd::distance(first2, last2)라고 하면 최대 S·N번의 비교(또는 p 적용)가 필요해요.

예제 (Example)

#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> v{0, 2, 3, 25, 5};
    std::vector<int> t{3, 19, 10, 2};

    auto result = std::find_first_of(v.begin(), v.end(), t.begin(), t.end());

    if (result == v.end())
        std::cout << "no element of v is equal to any element of t\n";
    else
        std::cout << "found a match at index " << std::distance(v.begin(), result) << '\n';
}

출력:

found a match at index 1

더 알아보기 (Learn more)

cppreference