std::find, std::find_if, std::find_if_not
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <algorithm> 中定义
|
||
template< class InputIt, class T > InputIt find( InputIt first, InputIt last, const T& value ); |
(1) | |
template< class InputIt, class UnaryPredicate > InputIt find_if( InputIt first, InputIt last, |
(2) | |
template< class InputIt, class UnaryPredicate > InputIt find_if_not( InputIt first, InputIt last, |
(3) | (C++11 起) |
[first, last)
满足特定的条件的范围内找到的第一个元素[first, last)
that satisfies specific criteria:find
搜索元素等于value
find
searches for an element equal to value
find_if
搜索元素的谓词p
回报truefind_if
searches for an element for which predicate p
returns truefind_if_not
搜索元素的谓词q
回报falsefind_if_not
searches for element for which predicate q
returns false目录 |
[编辑] 参数
first, last | - | 检查的元素
|
value | - | 值进行比较的元素
|
p | - | unary predicate which returns true 所需的元素 . The signature of the predicate function should be equivalent to the following: bool pred(const Type &a); The signature does not need to have const &, but the function must not modify the objects passed to it. |
q | - | unary predicate which returns false 所需的元素 . The signature of the predicate function should be equivalent to the following: bool pred(const Type &a); The signature does not need to have const &, but the function must not modify the objects passed to it. |
类型要求 | ||
-InputIt 必须满足 InputIterator 的要求。
|
[编辑] 返回值
last
.last
if no such element is found.[编辑] 复杂度
last
- first
谓词的应用last
- first
applications of the predicate[编辑] 可能的实现
版本一 |
---|
template<class InputIt, class T> InputIt find(InputIt first, InputIt last, const T& value) { for (; first != last; ++first) { if (*first == value) { return first; } } return last; } |
版本二 |
template<class InputIt, class UnaryPredicate> InputIt find_if(InputIt first, InputIt last, UnaryPredicate p) { for (; first != last; ++first) { if (p(*first)) { return first; } } return last; } |
版本三 |
template<class InputIt, class UnaryPredicate> InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q) { for (; first != last; ++first) { if (!q(*first)) { return first; } } return last; } |
template<class InputIt, class UnaryPredicate> InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q) { return std::find_if(first, last, std::not1(q)); } |
[编辑] 示例
#include <iostream> #include <algorithm> #include <vector> int main() { int n1 = 3; int n2 = 5; std::vector<int> v{0, 1, 2, 3, 4}; auto result1 = std::find(v.begin(), v.end(), n1); auto result2 = std::find(v.begin(), v.end(), n2); if (result1 != v.end()) { std::cout << "v contains: " << n1 << '\n'; } else { std::cout << "v does not contain: " << n1 << '\n'; } if (result2 != v.end()) { std::cout << "v contains: " << n2 << '\n'; } else { std::cout << "v does not contain: " << n2 << '\n'; } }
输出:
v contains: 3 v does not contain: 5
[编辑] 另请参阅
查找彼此相邻的两个相同(或其它的关系)的元素 (函数模板) | |
查找一定范围内最后出现的元素序列 (函数模板) | |
查找元素集合中的任意元素 (函数模板) | |
查找两个范围第一个不同元素的位置 (函数模板) | |
查找一个元素区间 (函数模板) |