C++的find和find_if函数
2017-03-10 10:25
37 查看
一、find()算法
[cpp] view
plain copy
print?
template<class InputIterator, class T>
InputIterator find ( InputIterator first, InputIterator last, const T& value )
{
for ( ;first!=last; first++) if ( *first==value ) break;
return first;
}
返回区间[first,end)中第一个值等于value的元素的位置。
如果没有找到匹配元素,则返回end。
复杂度:线性复杂度。最多比较次数是:元素的总个数。
程序实例:
下面的程序在int类型的vector中搜寻元素5和12,如果搜索到,就返回其位置if欧泽输出提示信息。
main.cpp(头文件algostuff.h和上一篇博客中的相同):
[cpp] view
plain copy
print?
#include "algostuff.h"
using namespace std;
int main()
{
vector<int> intVec;
INSERT_ELEMENTS(intVec,1,9);
vector<int>::iterator pos;
pos = find(intVec.begin(),intVec.end(),5);
if(pos != intVec.end())
cout << "The value 5 exists,and its position is " <<
distance(intVec.begin(),pos) + 1 << endl;
else
cout << "The value 4 not found!" << endl;
pos = find(intVec.begin(),intVec.end(),12);
if(pos != intVec.end())
cout << "The value 12 exists,and its position is " <<
distance(intVec.begin(),pos) + 1 << endl;
else
cout << "The value 12 not found!" << endl;
}
运行结果(头文件algostuff.h和上一篇博客中的相同):
![](http://my.csdn.net/uploads/201204/23/1335185138_1981.jpg)
二、find_if()算法
[cpp] view
plain copy
print?
template<class InputIterator, class Predicate>
InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred )
{
for ( ; first!=last ; first++ ) if ( pred(*first) ) break;
return first;
}
它在区间[first,end)中搜寻使一元判断式pred为true的第一个元素。
如果没找到,返回end。
程序实例:
下面程序找出第一个能够被3整除的元素,如果找到返回其位置。
main.cpp:
[cpp] view
plain copy
print?
#include "algostuff.h"
using namespace std;
int main()
{
vector<int> intVec;
INSERT_ELEMENTS(intVec,1,9);
vector<int>::iterator pos;
pos = find_if(intVec.begin(),intVec.end(),
not1(bind2nd(modulus<int>(),3)));
if(pos != intVec.end())
cout << "The value divided by 3 exists,and the first value's position is " <<
distance(intVec.begin(),pos) + 1 << endl;
else
cout << "The value divided by 3 not found!" << endl;
}
运行结果:
[cpp] view
plain copy
print?
template<class InputIterator, class T>
InputIterator find ( InputIterator first, InputIterator last, const T& value )
{
for ( ;first!=last; first++) if ( *first==value ) break;
return first;
}
返回区间[first,end)中第一个值等于value的元素的位置。
如果没有找到匹配元素,则返回end。
复杂度:线性复杂度。最多比较次数是:元素的总个数。
程序实例:
下面的程序在int类型的vector中搜寻元素5和12,如果搜索到,就返回其位置if欧泽输出提示信息。
main.cpp(头文件algostuff.h和上一篇博客中的相同):
[cpp] view
plain copy
print?
#include "algostuff.h"
using namespace std;
int main()
{
vector<int> intVec;
INSERT_ELEMENTS(intVec,1,9);
vector<int>::iterator pos;
pos = find(intVec.begin(),intVec.end(),5);
if(pos != intVec.end())
cout << "The value 5 exists,and its position is " <<
distance(intVec.begin(),pos) + 1 << endl;
else
cout << "The value 4 not found!" << endl;
pos = find(intVec.begin(),intVec.end(),12);
if(pos != intVec.end())
cout << "The value 12 exists,and its position is " <<
distance(intVec.begin(),pos) + 1 << endl;
else
cout << "The value 12 not found!" << endl;
}
运行结果(头文件algostuff.h和上一篇博客中的相同):
![](http://my.csdn.net/uploads/201204/23/1335185138_1981.jpg)
二、find_if()算法
[cpp] view
plain copy
print?
template<class InputIterator, class Predicate>
InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred )
{
for ( ; first!=last ; first++ ) if ( pred(*first) ) break;
return first;
}
它在区间[first,end)中搜寻使一元判断式pred为true的第一个元素。
如果没找到,返回end。
程序实例:
下面程序找出第一个能够被3整除的元素,如果找到返回其位置。
main.cpp:
[cpp] view
plain copy
print?
#include "algostuff.h"
using namespace std;
int main()
{
vector<int> intVec;
INSERT_ELEMENTS(intVec,1,9);
vector<int>::iterator pos;
pos = find_if(intVec.begin(),intVec.end(),
not1(bind2nd(modulus<int>(),3)));
if(pos != intVec.end())
cout << "The value divided by 3 exists,and the first value's position is " <<
distance(intVec.begin(),pos) + 1 << endl;
else
cout << "The value divided by 3 not found!" << endl;
}
运行结果:
![](http://my.csdn.net/uploads/201204/23/1335185584_6613.jpg)
相关文章推荐
- C++在成员函数中使用STL的find_if函数实例
- STL中的find_if函数
- 关于find_if函数
- java string.split()函数的c/c++两种实现——strtok_r及string.find()
- NDK do not find the standard C++ libraries
- 在成员函数中使用STL的find_if函数
- C++ debug "cannot find the file Debug\***.pch" & how to debug multi-projects under one solution
- C/C++:遍历子目录,枚举文件,FindFirstFile + FindNextFile
- C++学习笔记第一天:熟悉C++,解决“Cannot find or open the PDB file”问题
- c++ find
- 详解STL的find_if函数
- string::npos在 c++ 的find_first_of判断是什么意思
- find_if函数
- STL的find_if函数
- string::npos在 c++ 的find_first_of判断是什么意思
- C++中的vector&find_if
- C/C++:遍历子目录,枚举文件,FindFirstFile + FindNextFile
- C++ STL set::find的用法
- C和C++中FindClass用法区别
- C++中string类的成员函数find_first_not_of()