标签:
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,如果搜索到,就返回其位置,否则输出提示信息。
#include <iostream>
#include <algorithm>
#include <vector>
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 " <<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 " <<pos + 1 << endl;
else
cout << "The value 12 not found!" << endl;
}
template <class InputIterator, class Predicate>
InputIterator find_if(InputIterator first, InputIterator last,Predicate pred)
{
while (first != last && !pred(*first)) ++first;
return first;
}
find_if是一个模板函数,接受两个数据类型:InputItearator迭代器,Predicate用于比较数值的函数或者函数对象(仿函数)。 find_if对迭代器要求很低,只需要它支持自增操作即可。当前遍历到的记录符合条件与否,判断标准就是使得pred()为真。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool IsOdd (int i) {
return ((i%2)==1);
}
int main () {
vector<int> myvector;
vector<int>::iterator it;
myvector.push_back(10);
myvector.push_back(25);
myvector.push_back(40);
myvector.push_back(55);
it = find_if (myvector.begin(), myvector.end(), IsOdd);
cout << "The first odd value is " << *it << endl;
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class CTest
{
public:
bool IsOdd (int i) {
return ((i%2)==1);
}
int test () {
vector<int> myvector;
vector<int>::iterator it;
myvector.push_back(10);
myvector.push_back(25);
myvector.push_back(40);
myvector.push_back(55);
it = find_if (myvector.begin(), myvector.end(), std::bind1st(std::mem_fun(&CTest::IsOdd),this));
cout << "The first odd value is " << *it << endl;
return 0;
}
};
int main()
{
CTest t1;
t1.test();
return 0;
}
运行结果:
The first odd value is 25:
#include <vector>
#include <string>
struct value_t {
int a;
int b;
};
class vector_finder{
public:
vector_finder(const int a):m_i_a(a){}
bool operator ()(const std::vector<struct value_t>::value_type &value){
return value.a == m_i_a;
}
private:
int m_i_a;
};
int main(){
std::vector<struct value_t> my_vector;
struct value_t my_value;
my_value.a = 11; my_value.b = 1000;
my_vector.push_back(my_value);
my_value.a = 12; my_value.b = 1000;
my_vector.push_back(my_value);
my_value.a = 13; my_value.b = 1000;
my_vector.push_back(my_value);
my_value.a = 14; my_value.b = 1000;
my_vector.push_back(my_value);
std::vector<struct value_t>::iterator it = my_vector.end();
it = std::find_if(my_vector.begin(), my_vector.end(), vector_finder(13));
if (it == my_vector.end())
printf("not found\n");
else
printf("found value.a:%d value.b:%d\n", it->a, it->b);
getchar();
return 0;
}
标签:
原文地址:http://my.oschina.net/u/1398794/blog/501863