C++的find和find_if函数

一、find()算法

[cpp]  view plain  copy
 print ?
  1. template<class InputIterator, class T>  
  2.   InputIterator find ( InputIterator first, InputIterator last, const T& value )  
  3.   {  
  4.     for ( ;first!=last; first++) if ( *first==value ) break;  
  5.     return first;  
  6.   }  
返回区间[first,end)中第一个值等于value的元素的位置。

如果没有找到匹配元素,则返回end。

复杂度:线性复杂度。最多比较次数是:元素的总个数。

程序实例:

下面的程序在int类型的vector中搜寻元素5和12,如果搜索到,就返回其位置if欧泽输出提示信息。

main.cpp(头文件algostuff.h和上一篇博客中的相同):

[cpp]  view plain  copy
 print ?
  1. #include "algostuff.h"  
  2.   
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.     vector<int> intVec;  
  8.   
  9.     INSERT_ELEMENTS(intVec,1,9);  
  10.   
  11.     vector<int>::iterator pos;  
  12.     pos = find(intVec.begin(),intVec.end(),5);  
  13.   
  14.     if(pos != intVec.end())  
  15.         cout << "The value 5 exists,and its position is " <<  
  16.         distance(intVec.begin(),pos) + 1 << endl;  
  17.     else  
  18.         cout << "The value 4 not found!" << endl;  
  19.   
  20.     pos = find(intVec.begin(),intVec.end(),12);  
  21.   
  22.     if(pos != intVec.end())  
  23.         cout << "The value 12 exists,and its position is " <<  
  24.         distance(intVec.begin(),pos) + 1 << endl;  
  25.     else  
  26.         cout << "The value 12 not found!" << endl;  
  27. }  
运行结果 (头文件 algostuff.h 和上一篇博客中的相同)


二、find_if()算法

[cpp]  view plain  copy
 print ?
  1. template<class InputIterator, class Predicate>  
  2.   InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred )  
  3.   {  
  4.     for ( ; first!=last ; first++ ) if ( pred(*first) ) break;  
  5.     return first;  
  6.   }  
它在区间[first,end)中搜寻使一元判断式pred为true的第一个元素。

如果没找到,返回end。

程序实例:

下面程序找出第一个能够被3整除的元素,如果找到返回其位置。

main.cpp:

[cpp]  view plain  copy
 print ?
  1. #include "algostuff.h"  
  2.   
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.     vector<int> intVec;  
  8.   
  9.     INSERT_ELEMENTS(intVec,1,9);  
  10.   
  11.     vector<int>::iterator pos;  
  12.     pos = find_if(intVec.begin(),intVec.end(),  
  13.         not1(bind2nd(modulus<int>(),3)));  
  14.   
  15.     if(pos != intVec.end())  
  16.         cout << "The value divided by 3 exists,and the first value's position is " <<  
  17.         distance(intVec.begin(),pos) + 1 << endl;  
  18.     else  
  19.         cout << "The value divided by 3 not found!" << endl;  
  20. }  
运行结果:

C++中,`std::find_if` 是一个算法函数,用于在给定范围内查找满足指定条件的元素。该函数需要三个参数:范围的起始迭代器、范围的结束迭代器和一个谓词(条件)函数。 以下是 `std::find_if` 的函数签名: ```cpp template<class InputIt, class UnaryPredicate> InputIt find_if(InputIt first, InputIt last, UnaryPredicate p); ``` 其中: - `InputIt` 是迭代器类型,表示范围的起始和结束位置。 - `UnaryPredicate` 是一个可调用对象,用于确定元素是否满足条件。它接受一个参数并返回一个 `bool` 类型的结果。 以下是一个使用 `std::find_if` 函数的示例代码,演示如何查找一个数组中第一个大于 5 的元素: ```cpp #include <iostream> #include <vector> #include <algorithm> bool isGreaterThan5(int num) { return num > 5; } int main() { std::vector<int> numbers = {2, 4, 6, 8, 10}; auto it = std::find_if(numbers.begin(), numbers.end(), isGreaterThan5); if (it != numbers.end()) { std::cout << "First element greater than 5: " << *it << std::endl; } else { std::cout << "No element greater than 5 found." << std::endl; } return 0; } ``` 输出结果为: ``` First element greater than 5: 6 ``` 在这个示例中,我们定义了一个 `isGreaterThan5` 函数作为谓词,用于确定元素是否大于 5。然后,我们使用 `std::find_if` 函数在 `numbers` 向量中查找第一个满足条件的元素,并将结果存储在迭代器 `it` 中。最后,我们输出找到的元素(如果存在)或相应的提示信息。 你可以根据自己的需求编写不同的谓词函数来满足不同的查找条件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值