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) | (since C++11) | 
[first, last)范围内满足特定条件的第一个元素:[first, last) that satisfies specific criteria:- 1.find查找等于value的元素原文:1.findsearches for an element equal tovalue
- 2.find_if查找使谓词p返回true的元素原文:2.find_ifsearches for an element for which predicatepreturns true
- 3.find_if_not查找使谓词q返回false的元素原文:3.find_if_notsearches for element for which predicateqreturns 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
[编辑] 另请参阅
| 查找彼此相邻的两个相同(或其它的关系)的元素 (函数模板) | |
| 查找一定范围内最后出现的元素序列 (函数模板) | |
| 查找元素集合中的任意元素 (函数模板) | |
| 查找两个范围第一个不同元素的位置 (函数模板) | |
| 查找一个元素区间 (函数模板) | 


