std::find_first_of
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <algorithm> 中定义
|
||
template< class ForwardIt1, class ForwardIt2 > ForwardIt1 find_first_of( ForwardIt1 first, ForwardIt1 last, |
(1) | (至 C++11) (C++11 起) |
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > ForwardIt1 find_first_of( ForwardIt1 first, ForwardIt1 last, |
(2) | (至 C++11) (C++11 起) |
[first, last)
[s_first, s_last)
。的第一个版本使用operator==
比较的元素,第二个版本使用给定的二元谓词p
. [first, last)
for any of the elements in the range [s_first, s_last)
. The first version uses operator==
to compare the elements, the second version uses the given binary predicate p
. 目录 |
[编辑] 参数
first, last | - | 检查的元素
|
s_first, s_last | - | 的元素的范围搜索
|
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); The signature does not need to have const &, but the function must not modify the objects passed to it. |
类型要求 | ||
-InputIt 必须满足 InputIterator 的要求。
| ||
-ForwardIt1 必须满足 ForwardIterator 的要求。
| ||
-ForwardIt2 必须满足 ForwardIterator 的要求。
|
[编辑] 返回值
[first, last)
是相等的元素的范围内[s_first; s_last)
的范围中的第一个元素。如果没有这样的元素被发现,last
返回.[first, last)
that is equal to an element from the range [s_first; s_last)
. If no such element is found, last
is returned.[编辑] 复杂度
(S*N)
比较S = distance(s_first, s_last)N = distance(first, last).(S*N)
comparisons where S = distance(s_first, s_last) and N = distance(first, last).[编辑] 可能的实现
版本一 |
---|
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; } |
版本二 |
template<class InputIt, class ForwardIt, class BinaryPredicate> InputIt find_first_of(InputIt first, InputIt last, ForwardIt s_first, ForwardIt s_last, BinaryPredicate 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> int main() { std::vector<int> v{0, 2, 3, 25, 5}; std::vector<int> t{3, 19, 10, 2}; auto result = std::find_first_of(v.begin(), v.end(), t.begin(), t.end()); if (result == v.end()) { std::cout << "no elements of v were equal to 3, 19, 10 or 2\n"; } else { std::cout << "found a match at " << std::distance(v.begin(), result) << "\n"; } }
输出:
found a match at 1
[编辑] 另请参阅
(C++11) |
查找满足特定条件的第一个元素 (函数模板) |