std::find_first_of
定义于头文件 <algorithm> |
||
template< class InputIt, class ForwardIt > InputIt find_first_of( InputIt first, InputIt last, |
(1) | (C++20 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > ForwardIt1 find_first_of( ExecutionPolicy&& policy, |
(2) | (C++17 起) |
template< class InputIt, class ForwardIt, class BinaryPred > InputIt find_first_of( InputIt first, InputIt last, |
(3) | (C++20 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPred > |
(4) | (C++17 起) |
在范围 [
first,
last)
中搜索范围 [
s_first,
s_last)
中的任何元素。
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true。 |
(C++20 前) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> 为 true。 |
(C++20 起) |
目录 |
[编辑] 参数
first, last | - | 定义要检查的元素范围的迭代器对 |
s_first, s_last | - | 定义要搜索元素的范围的迭代器对 |
policy | - | 要使用的 执行策略 |
p | - | 二元谓词,如果元素应被视为相等,则返回 true。 谓词函数的签名应等效于以下内容: bool pred(const Type1 &a, const Type2 &b); 尽管函数签名不需要带有 const &,但该函数不得修改传递给它的对象,并且必须能够接受 `Type1` 和 `Type2` 类型(可能为 const)的所有值,无论其 值类别如何(因此,不允许使用 Type1 &,也不允许使用 Type1,除非对于 `Type1`,移动等同于复制(C++11 起))。 |
类型要求 | ||
-InputIt 必须满足 LegacyInputIterator 的要求。 | ||
-ForwardIt 必须满足 LegacyForwardIterator 的要求。 | ||
-ForwardIt1 必须满足 LegacyForwardIterator 的要求。 | ||
-ForwardIt2 必须满足 LegacyForwardIterator 的要求。 | ||
-BinaryPred 必须满足 BinaryPredicate 的要求。 |
[编辑] 返回值
指向范围 [
first,
last)
中第一个与范围 [
s_first,
s_last)
中某个元素相等的元素的迭代器。
如果 [
s_first,
s_last)
为空,或者没有找到这样的元素,则返回 last。
[编辑] 复杂度
给定 N 为 std::distance(first, last),S 为 std::distance(s_first, s_last)
[编辑] 异常
带有模板参数 ExecutionPolicy
的重载按如下方式报告错误
- 如果在算法中调用的函数抛出异常,并且
ExecutionPolicy
是 标准策略之一,则调用 std::terminate。对于任何其他ExecutionPolicy
,行为是实现定义的。 - 如果算法未能分配内存,则抛出 std::bad_alloc。
[编辑] 可能实现
find_first_of (1) |
---|
template<class InputIt, class ForwardIt> InputIt find_first_of(InputIt first, InputIt last, ForwardIt s_first, ForwardIt s_last) { for (; first != last; ++first) for (ForwardIt it = s_first; it != s_last; ++it) if (*first == *it) return first; return last; } |
find_first_of (3) |
template<class InputIt, class ForwardIt, class BinaryPred> InputIt find_first_of(InputIt first, InputIt last, ForwardIt s_first, ForwardIt s_last, BinaryPred p) { for (; first != last; ++first) for (ForwardIt it = s_first; it != s_last; ++it) if (p(*first, *it)) return first; return last; } |
[编辑] 示例
以下代码在整数向量中搜索任何指定的整数
#include <algorithm> #include <iostream> #include <vector> auto print_sequence = [](const auto id, const auto& seq, int pos = -1) { std::cout << id << "{ "; for (int i{}; auto const& e : seq) { const bool mark{i == pos}; std::cout << (i++ ? ", " : ""); std::cout << (mark ? "[ " : "") << e << (mark ? " ]" : ""); } std::cout << " }\n"; }; int main() { const std::vector<int> v{0, 2, 3, 25, 5}; const auto t1 = {19, 10, 3, 4}; const auto t2 = {1, 6, 7, 9}; auto find_any_of = [](const auto& v, const auto& t) { const auto result = std::find_first_of(v.begin(), v.end(), t.begin(), t.end()); if (result == v.end()) { std::cout << "No elements of v are equal to any element of "; print_sequence("t = ", t); print_sequence("v = ", v); } else { const auto pos = std::distance(v.begin(), result); std::cout << "Found a match (" << *result << ") at position " << pos; print_sequence(", where t = ", t); print_sequence("v = ", v, pos); } }; find_any_of(v, t1); find_any_of(v, t2); }
输出
Found a match (3) at position 2, where t = { 19, 10, 3, 4 } v = { 0, 2, [ 3 ], 25, 5 } No elements of v are equal to any element of t = { 1, 6, 7, 9 } v = { 0, 2, 3, 25, 5 }
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 576 | C++98 | first 和 last 需要是 LegacyForwardIterators | 它们只需要是 LegacyInputIterators |
LWG 1205 | C++98 | 如果 [ s_first, s_last) 为空,返回值不明确 |
在这种情况下返回 last |
[编辑] 另请参阅
(C++11) |
寻找第一个满足特定条件的元素 (函数模板) |
(C++20) |
搜索一组元素中的任何一个 (算法函数对象) |