다음을 통해 공유


find_if

범위에서 지정된 조건을 만족하는 요소가 첫 번째 나타나는 위치를 찾습니다.

template<class InputIterator, class Predicate> InputIterator find_if(InputIterator first, InputIterator last,        Predicate pred);

매개 변수

  • first
    검색할 범위에서 첫 번째 요소 위치의 주소를 지정하는 입력 반복기입니다.

  • last
    검색할 범위에서 마지막 요소 하나 다음의 위치 주소를 지정하는 입력 반복기입니다.

  • pred
    검색 중인 요소가 충족하는 조건을 정의하는 람다 식 또는 사용자 정의 조건자 함수 개체입니다. 조건자는 인수 하나를 사용하며 true(충족) 또는 false(미충족)를 반환합니다. pred의 서명은 bool pred(const T& arg);여야 합니다. 여기서 T는 역참조 시 InputIterator를 암시적으로 변환할 수 있는 형식입니다. const 키워드는 함수 개체 또는 람다가 인수를 수정하지 않아야 함을 나타내기 위한 용도로만 표시되어 있습니다.

반환 값

조건자로 지정된 조건을 충족하는 범위 내 첫 번째 요소를 참조하는 입력 반복기입니다. 이 경우 조건자는 true를 반환합니다. 조건자를 충족하는 요소가 없으면 last를 반환합니다.

설명

이 템플릿 함수는 find 알고리즘을 일반화한 것으로, "equals a specific value" 조건자를 임의의 조건자로 바꿉니다. 이 조건자와 논리적으로 반대되는 항목(조건자를 충족하지 않는 첫 번째 요소를 찾음)은 find_if_not을 참조하세요.

예제

// cl.exe /W4 /nologo /EHsc /MTd
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

template <typename S> void print(const S& s) {
    for (const auto& p : s) {
        cout << "(" << p << ") ";
    }
    cout << endl;
}

// Test std::find()
template <class InputIterator, class T>
void find_print_result(InputIterator first, InputIterator last, const T& value) {

    // call <algorithm> std::find()
    auto p = find(first, last, value);

    cout << "value " << value;
    if (p == last) {
        cout << " not found." << endl;
    } else {
        cout << " found." << endl;
    }
}

// Test std::find_if()
template <class InputIterator, class Predicate>
void find_if_print_result(InputIterator first, InputIterator last,
    Predicate Pred, const string& Str) {

    // call <algorithm> std::find_if()
    auto p = find_if(first, last, Pred);

    if (p == last) {
        cout << Str << " not found." << endl;
    } else {
        cout << "first " << Str << " found: " << *p << endl;
    }
}

// Function to use as the UnaryPredicate argument to find_if() in this example
bool is_odd_int(int i) {
    return ((i % 2) != 0);
}

int main()
{
    // Test using a plain old array.
    const int x[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    cout << "array x[] contents: ";
    print(x);
    // Using non-member std::begin()/std::end() to get input iterators for the plain old array.
    cout << "Test std::find() with array..." << endl;
    find_print_result(begin(x), end(x), 10);
    find_print_result(begin(x), end(x), 42);
    cout << "Test std::find_if() with array..." << endl;
    find_if_print_result(begin(x), end(x), is_odd_int, "odd integer"); // function name
    find_if_print_result(begin(x), end(x), // lambda
        [](int i){ return ((i % 2) == 0); }, "even integer");

    // Test using a vector.
    vector<int> v;
    for (int i = 0; i < 10; ++i) {
        v.push_back((i + 1) * 10);
    }
    cout << endl << "vector v contents: ";
    print(v);
    cout << "Test std::find() with vector..." << endl;
    find_print_result(v.begin(), v.end(), 20);
    find_print_result(v.begin(), v.end(), 12);
    cout << "Test std::find_if() with vector..." << endl;
    find_if_print_result(v.begin(), v.end(), is_odd_int, "odd integer");
    find_if_print_result(v.begin(), v.end(), // lambda
        [](int i){ return ((i % 2) == 0); }, "even integer");
}

출력

                               

요구 사항

헤더: <algorithm>

네임스페이스: std

참고 항목

참조

<algorithm>

adjacent_find

find(STL)

find_if_not

find_end

mismatch

search