std::find, find_if, find_if_not,find_end,find_first_of,adjacent_find

Defined in header 

template< class InputIt, class ForwardIt >

InputIt find_first_of( InputIt first, InputIt last,

                       ForwardIt s_first, ForwardIt s_last );
(1) (constexpr since C++20)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >

ForwardIt1 find_first_of( ExecutionPolicy&& policy,
                          ForwardIt1 first, ForwardIt1 last,

                          ForwardIt2 s_first, ForwardIt2 s_last );
(2) (since C++17)
template< class InputIt, class ForwardIt, class BinaryPred >

InputIt find_first_of( InputIt first, InputIt last,
                       ForwardIt s_first, ForwardIt s_last,

                       BinaryPred p );
(3) (constexpr since C++20)
template< class ExecutionPolicy,

          class ForwardIt1, class ForwardIt2, class BinaryPred >
ForwardIt1 find_first_of( ExecutionPolicy&& policy,
                          ForwardIt1 first, ForwardIt last,
                          ForwardIt2 s_first, ForwardIt2 s_last,

                          BinaryPred p );
(4) (since C++17)

Searches the range [firstlast) for any of the elements in the range [s_firsts_last).

1) Elements are compared using operator==.

3) Elements are compared using the given binary predicate p.

2,4) Same as (1,3), but executed according to policy.

 These overloads participate in overload resolution only if all following conditions are satisfied:

std::is_execution_policy_v> is true.

(until C++20)

std::is_execution_policy_v> is true.

(since C++20)

Parameters

first, last - the pair of iterators defining the range of elements to examine
s_first, s_last - the pair of iterators defining the range of elements to search for
policy - the execution policy to use
p - binary predicate which returns ​true if the elements should be treated as equal.

The signature of the predicate function should be equivalent to the following:

 bool pred(const Type1 &a, const Type2 &b);

While the signature does not need to have const &, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const) Type1 and Type2 regardless of value category (thus, Type1 & is not allowed, nor is Type1 unless for Type1 a move is equivalent to a copy(since C++11)).
The types Type1 and Type2 must be such that objects of types ForwardIt1 and ForwardIt2 can be dereferenced and then implicitly converted to Type1 and Type2 respectively.​

Return value

Iterator to the first element in the range [firstlast) that is equal to an element from the range [s_firsts_last).

If [s_firsts_last) is empty or if no such element is found, last is returned.

std::find` 用于在范围内查找第一个等于指定值的元素。

std::find_if` 用于在范围内查找第一个满足指定条件的元素。

std::find_if_not` 用于在范围内查找第一个不满足指定条件的元素。

std::find_first_of` 用于在范围内查找第一个等于另一个范围中任一元素的元素。

std::find_end` 用于在范围内查找最后一个子序列的起始位置。

std::adjacent_find` 用于在范围内查找第一个相邻的重复元素对。

std::find


`std::find` 用于在范围内查找第一个等于指定值的元素。

语法
template
InputIt find(InputIt first, InputIt last, const T& value);
```

示例
 

#include 
#include 
#include 

int main() {
    std::vector vec = {1, 2, 3, 4, 5};

    auto it = std::find(vec.begin(), vec.end(), 3);

    if (it != vec.end()) {
        std::cout << "Element found at position: " << std::distance(vec.begin(), it) << std::endl;
    } else {
        std::cout << "Element not found" << std::endl;
    }

    return 0;
}

std::find_if


`std::find_if` 用于在范围内查找第一个满足指定条件的元素。

语法
template
InputIt find_if(InputIt first, InputIt last, UnaryPredicate p);
 

 示例
 

#include 
#include 
#include 

bool isEven(int n) {
    return n % 2 == 0;
}

int main() {
    std::vector vec = {1, 3, 5, 6, 7};

    auto it = std::find_if(vec.begin(), vec.end(), isEven);

    if (it != vec.end()) {
        std::cout << "First even element found at position: " << std::distance(vec.begin(), it) << std::endl;
    } else {
        std::cout << "No even element found" << std::endl;
    }

    return 0;
}

std::find_if_not


`std::find_if_not` 用于在范围内查找第一个不满足指定条件的元素。

语法

template
InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate p);

示例
 

#include 
#include 
#include 

bool isOdd(int n) {
    return n % 2 != 0;
}

int main() {
    std::vector vec = {1, 3, 5, 6, 7};

    auto it = std::find_if_not(vec.begin(), vec.end(), isOdd);

    if (it != vec.end()) {
        std::cout << "First non-odd element found at position: " << std::distance(vec.begin(), it) << std::endl;
    } else {
        std::cout << "All elements are odd" << std::endl;
    }

    return 0;
}

std::find_end

`std::find_end` 用于在范围内查找最后一个子序列的起始位置。

语法

Defined in header 

template< class ForwardIt1, class ForwardIt2 >

ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last,

                     ForwardIt2 s_first, ForwardIt2 s_last );
(1) (constexpr since C++20)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >

ForwardIt1 find_end( ExecutionPolicy&& policy,
                     ForwardIt1 first, ForwardIt1 last,

                     ForwardIt2 s_first, ForwardIt2 s_last );
(2) (since C++17)
template< class ForwardIt1, class ForwardIt2, class BinaryPred >

ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last,
                     ForwardIt2 s_first, ForwardIt2 s_last,

                     BinaryPred p );
(3) (constexpr since C++20)
template< class ExecutionPolicy,

          class ForwardIt1, class ForwardIt2, class BinaryPred >
ForwardIt1 find_end( ExecutionPolicy&& policy,
                     ForwardIt1 first, ForwardIt1 last,
                     ForwardIt2 s_first, ForwardIt2 s_last,

                     BinaryPred p );

示例
 

#include 
#include 
#include 
#include 
 
auto print_result = [](auto result, const auto& v)
{
    result == v.end()
        ? std::cout << "Sequence not found\n"
        : std::cout << "Last occurrence is at: " << std::distance(v.begin(), result)
                    << '\n';
};
 
int main()
{
    const auto v = {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4};
 
    for (auto const& x : {std::array{1, 2, 3}, {4, 5, 6}})
    {
        auto iter = std::find_end(v.begin(), v.end(), x.begin(), x.end()); // overload (1)
        print_result(iter, v);
    }
 
    for (auto const& x : {std::array{-1, -2, -3}, {-4, -5, -6}})
    {
        auto iter = std::find_end(v.begin(), v.end(), x.begin(), x.end(), // overload (3)
                                  [](int x, int y)
                                  {
                                      return std::abs(x) == std::abs(y);
                                  });
        print_result(iter, v);
    }
}

std::find_first_of

`std::find_first_of` 用于在范围内查找第一个等于另一个范围中任一元素的元素。

语法

Defined in header 

template< class InputIt, class ForwardIt >

InputIt find_first_of( InputIt first, InputIt last,

                       ForwardIt s_first, ForwardIt s_last );
(1) (constexpr since C++20)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >

ForwardIt1 find_first_of( ExecutionPolicy&& policy,
                          ForwardIt1 first, ForwardIt1 last,

                          ForwardIt2 s_first, ForwardIt2 s_last );
(2) (since C++17)
template< class InputIt, class ForwardIt, class BinaryPred >

InputIt find_first_of( InputIt first, InputIt last,
                       ForwardIt s_first, ForwardIt s_last,

                       BinaryPred p );
(3) (constexpr since C++20)
template< class ExecutionPolicy,

          class ForwardIt1, class ForwardIt2, class BinaryPred >
ForwardIt1 find_first_of( ExecutionPolicy&& policy,
                          ForwardIt1 first, ForwardIt last,
                          ForwardIt2 s_first, ForwardIt2 s_last,

                          BinaryPred p );

Searches the range [firstlast) for any of the elements in the range [s_firsts_last).

1) Elements are compared using operator==.

3) Elements are compared using the given binary predicate p.

2,4) Same as (1,3), but executed according to policy.

 These overloads participate in overload resolution only if all following conditions are satisfied:

first, last - the pair of iterators defining the range of elements to examine
s_first, s_last - the pair of iterators defining the range of elements to search for
policy - the execution policy to use
p - binary predicate which returns ​true if the elements should be treated as equal.

The signature of the predicate function should be equivalent to the following:

 bool pred(const Type1 &a, const Type2 &b);

return value:

Iterator to the first element in the range [firstlast) that is equal to an element from the range [s_firsts_last).

If [s_firsts_last) is empty or if no such element is found, last is returned.

示例

#include 
#include 
#include 

int main() 
{
    std::vector vec = {1, 2, 3, 4, 5};
    std::vector targets = {3, 7, 9};

    auto it = std::find_first_of(vec.begin(), vec.end(), targets.begin(), targets.end());

    if (it != vec.end()) 
    {
        std::cout << "First matching element found at position: " << std::distance(vec.begin(), it) << std::endl;
    } 
    else 
    {
        std::cout << "No matching element found" << std::endl;
    }

    return 0;
}

std::adjacent_find
`std::adjacent_find` 用于在范围内查找第一个相邻的重复元素对。

语法

Defined in header 

template< class ForwardIt >
ForwardIt adjacent_find( ForwardIt first, ForwardIt last );

(1) (constexpr since C++20)
template< class ExecutionPolicy, class ForwardIt >

ForwardIt adjacent_find( ExecutionPolicy&& policy,

                         ForwardIt first, ForwardIt last );
(2) (since C++17)
template< class ForwardIt, class BinaryPred >

ForwardIt adjacent_find( ForwardIt first, ForwardIt last,

                         BinaryPred p );
(3) (constexpr since C++20)
template< class ExecutionPolicy, class ForwardIt, class BinaryPred >

ForwardIt adjacent_find( ExecutionPolicy&& policy,
                         ForwardIt first, ForwardIt last,

                         BinaryPred p );
(4) (since C++17)

Searches the range [firstlast) for two consecutive equal elements.

1) Elements are compared using operator==.

3) Elements are compared using the given binary predicate p.

parameters:

first, last - the pair of iterators defining the range of elements to examine
policy - the execution policy to use
p - binary predicate which returns ​true if the elements should be treated as equal.

The signature of the predicate function should be equivalent to the following:

 bool pred(const Type1 &a, const Type2 &b);

While the signature does not need to have const &, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const) Type1 and Type2 regardless of value category (thus, Type1 & is not allowed, nor is Type1 unless for Type1 a move is equivalent to a copy(since C++11)).
The types Type1 and Type2 must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to both of them.​

Type requirements
-ForwardIt must meet the requirements of LegacyForwardIterator.
-BinaryPred must meet the requirements of BinaryPredicate.

return value:

An iterator to the first of the first pair of identical elements, that is, the first iterator it such that *it == *(it + 1) for (1,2) or p(*it, *(it + 1)) != false for (3,4).

If no such elements are found, last is returned.

(3,4)可能的实现

template
ForwardIt adjacent_find(ForwardIt first, ForwardIt last, BinaryPred p)
{
    if (first == last)
        return last;
 
    ForwardIt next = first;
    ++next;
 
    for (; next != last; ++next, ++first)
        if (p(*first, *next)) //pred 为true时返回
            return first;
 
    return last;
}

 示例
 

#include 
#include 
#include 
#include 
 
int main()
{
    std::vector v1{0, 1, 2, 3, 40, 40, 41, 41, 5};
 
    auto i1 = std::adjacent_find(v1.begin(), v1.end());
 
    if (i1 == v1.end())
        std::cout << "No matching adjacent elements\n";
    else
        std::cout << "The first adjacent pair of equal elements is at "
                  << std::distance(v1.begin(), i1) << ", *i1 = "
                  << *i1 << '\n';
 
    auto i2 = std::adjacent_find(v1.begin(), v1.end(), std::greater());
    if (i2 == v1.end())
        std::cout << "The entire vector is sorted in ascending order\n";
    else
        std::cout << "The last element in the non-decreasing subsequence is at "
                  << std::distance(v1.begin(), i2) << ", *i2 = " << *i2 << '\n';
}

The first adjacent pair of equal elements is at 4, *i1 = 40
The last element in the non-decreasing subsequence is at 7, *i2 = 41

这些示例展示了如何使用这些查找算法来在范围内查找特定元素或子序列。

你可能感兴趣的:(C++20,C++,adjacent_find,find_if_not,find_end,find_first_of)