[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/algorithm/find_if [Back]  [Original]

std::find, std::find_if, std::find_if_not cppreference.com
cppreference.com

std::find, std::find_if, std::find_if_not

cppreference.com
 
C++
(C++20)
(C++20)
(C++11)
(C++20)
/
(C++11)
(C++11)
(C++11)
(C++17)
 
(C++20)
, ranges::copy, ranges::sort, ...
(C++17)
(C++11)(C++11)(C++11)
(C++17)
(C++11)
( )
(C++11)
/
(C++11)
(C++17)

(C++17)
(C++17)
(C++17)
C
 
<tbody> </tbody>
template< class InputIt, class T > InputIt find( InputIt first, InputIt last, const T& value );
(1)
template< class InputIt, class UnaryPredicate > InputIt find_if( InputIt first, InputIt last, UnaryPredicate p );
(2)
template< class InputIt, class UnaryPredicate > InputIt find_if_not( InputIt first, InputIt last, UnaryPredicate q );
(3) ( C++11)

[first, last) , :

1. find , value

2. find_if , p true

3. find_if_not , q false

[firstlast)
value
p , true .

:

bool pred(const Type &a);

const & , .
Type , InputIt Type.

q , false .

:

bool pred(const Type &a);

const & , .
Type , InputIt Type.

-
InputIt InputIterator.

, , last, .

last - first .

template<class InputIt, class T>
InputIt find(InputIt first, InputIt last, const T& value)
{
    for (; first != last; ++first) {
        if (*first == value) {
            return first;
        }
    }
    return last;
}
template<class InputIt, class UnaryPredicate>
InputIt find_if(InputIt first, InputIt last, UnaryPredicate p)
{
    for (; first != last; ++first) {
        if (p(*first)) {
            return first;
        }
    }
    return last;
}
template<class InputIt, class UnaryPredicate>
InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q)
{
    for (; first != last; ++first) {
        if (!q(*first)) {
            return first;
        }
    }
    return last;
}

C++11, std::find_if_not std::find_if .

template<class InputIt, class UnaryPredicate>
InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q)
{
    return std::find_if(first, last, std::not1(q));
}

.

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

int main()
{
    int n1 = 3;
    int n2 = 5;

    std::vector<int> v{0, 1, 2, 3, 4};

    auto result1 = std::find(v.begin(), v.end(), n1);
    auto result2 = std::find(v.begin(), v.end(), n2);

    if (result1 != v.end()) {
        std::cout << "v : " << n1 << '\n';
    } else {
        std::cout << "v  : " << n1 << '\n';
    }

    if (result2 != v.end()) {
        std::cout << "v : " << n2 << '\n';
    } else {
        std::cout << "v  : " << n2 << '\n';
    }
}

:

v : 3
v  : 5

.

, ( )
( ) []

( ) []

( ) []
,
( ) []

( ) []

Web Proxy Viewer  |  New URL  |  Original Page