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, |
(2) | (since C++17) |
template< class InputIt, class ForwardIt, class BinaryPred > InputIt find_first_of( InputIt first, InputIt last, |
(3) | (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPred > |
(4) | (since C++17) |
Searches the range [
first,
last)
for any of the elements in the range [
s_first,
s_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 |
(until C++20) |
std::is_execution_policy_v |
(since C++20) |
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) |
Iterator to the first element in the range [
first,
last)
that is equal to an element from the range [
s_first,
s_last)
.
If [
s_first,
s_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` 用于在范围内查找第一个等于指定值的元素。
语法
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` 用于在范围内查找第一个满足指定条件的元素。
语法
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` 用于在范围内查找第一个不满足指定条件的元素。
语法
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` 用于在范围内查找最后一个子序列的起始位置。
语法
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, |
(2) | (since C++17) |
template< class ForwardIt1, class ForwardIt2, class BinaryPred > ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last, |
(3) | (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPred > |
示例
#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` 用于在范围内查找第一个等于另一个范围中任一元素的元素。
语法
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, |
(2) | (since C++17) |
template< class InputIt, class ForwardIt, class BinaryPred > InputIt find_first_of( InputIt first, InputIt last, |
(3) | (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPred > |
Searches the range [
first,
last)
for any of the elements in the range [
s_first,
s_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 [
first,
last)
that is equal to an element from the range [
s_first,
s_last)
.
If [
s_first,
s_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 > |
(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, |
(4) | (since C++17) |
Searches the range [
first,
last)
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) |
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)可能的实现
templateForwardIt 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
这些示例展示了如何使用这些查找算法来在范围内查找特定元素或子序列。