std::find_first_of
来自cppreference.com
| 在标头 <algorithm> 定义
|
||
| (1) | (C++20 起为 constexpr) | |
| (2) | (C++20 起为 constexpr) | |
| |
(3) | (C++17 起) |
| |
(4) | (C++17 起) |
在源范围 [first1, last1) 中搜索任何在目标范围 [first2, last2) 中的元素。
1) 用
operator== 比较元素。2) 用给定的二元谓词
p 比较元素。3,4) 同 (1,2),但按照
policy 执行。 这些重载只有在以下表达式的值是
true 时才会参与重载决议:
|
|
(C++20 前) |
|
|
(C++20 起) |
参数
| first1, last1 | - | 表示源范围的迭代器对 |
| first2, last2 | - | 表示目标范围的迭代器对 |
| p | - | 若元素应被当做相等则返回 true 的二元谓词谓词函数的签名应等价于如下:
虽然签名不必有 |
| policy | - | 所用的执行策略 |
| 类型要求 | ||
-InputIt 必须满足老式输入迭代器 (LegacyInputIterator) 。
| ||
-ForwardIt 必须满足老式向前迭代器 (LegacyForwardIterator) 。
| ||
-ForwardIt1 必须满足老式向前迭代器 (LegacyForwardIterator) 。
| ||
-ForwardIt2 必须满足老式向前迭代器 (LegacyForwardIterator) 。
| ||
-BinaryPredicate 必须满足二元谓词 (BinaryPredicate) 。
| ||
返回值
指向源范围中首个与目标范围中的某个元素匹配的元素的迭代器。
如果目标范围为空或找不到这种元素,那么就会返回 last1。
复杂度
给定 N1 为 std::distance(first1, last1),N2 为 std::distance(first2, last2):
1) 最多应用 N1⋅N2 次
operator== 进行比较。2) 最多应用 N1⋅N2 次
p。3) 应用 𝓞(N1⋅N2) 次
operator== 进行比较。4) 应用 𝓞(N1⋅N2) 次
p。异常
3,4) 在执行过程中:
- 如果并行化所需的临时内存资源不可用,那么就会抛出 std::bad_alloc。
- 如果在通过算法实参访问对象时抛出了未捕获的异常,那么行为由执行策略决定(标准策略会调用 std::terminate)。
可能的实现
| find_first_of (1) |
|---|
template<class InputIt, class ForwardIt>
InputIt find_first_of(InputIt first1, InputIt last1,
ForwardIt first2, ForwardIt last2)
{
for (; first1 != last1; ++first1)
for (ForwardIt it = first2; it != last2; ++it)
if (*first1 == *it)
return first1;
return last1;
}
|
| find_first_of (2) |
template<class InputIt, class ForwardIt, class BinaryPred>
InputIt find_first_of(InputIt first1, InputIt last1,
ForwardIt first2, ForwardIt last2,
BinaryPred p)
{
for (; first1 != last1; ++first1)
for (ForwardIt it = first2; it != last2; ++it)
if (p(*first1, *it))
return first1;
return last1;
}
|
示例
下列代码在包含整数的 vector 中搜索任何一个指定的整数:
运行此代码
#include <algorithm>
#include <iostream>
#include <vector>
auto print_sequence = [](const auto id, const auto& seq, int pos = -1)
{
std::cout << id << "{ ";
for (int i{}; const auto& 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 << "v 和 t 中没有相等的元素\n";
print_sequence("t = ", t);
print_sequence("v = ", v);
}
else
{
const auto pos = std::distance(v.begin(), result);
std::cout << "在位置 " << pos << " 找到匹配 (" << *result << ")\n";
print_sequence("t = ", t);
print_sequence("v = ", v, pos);
}
};
find_any_of(v, t1);
find_any_of(v, t2);
}
输出:
在位置 2 找到匹配 (3)
t = { 19, 10, 3, 4 }
v = { 0, 2, [ 3 ], 25, 5 }
v 和 t 中没有相等的元素
t = { 1, 6, 7, 9 }
v = { 0, 2, 3, 25, 5 }
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 576 | C++98 | first1 和 last1 需要是老式向前迭代器 (LegacyForwardIterator)
|
只需要是老式输入迭代器 (LegacyInputIterator) |
| LWG 1205 | C++98 | 目标范围为空时返回值不明确 | 此时会返回 last1
|
参阅
(C++20) |
搜索一组元素中任一元素 (算法函数对象) |
(C++11) |
查找首个满足特定条件的元素 (函数模板 & 算法函数对象) |
(C++20)(C++20)(C++20) |