容器find_if函数定义和其第三个参数重载的疑问

简单明了,这个是cpluscpus 对find_if的定义:

1
2
3
4
5
6
template<class InputIterator, class Predicate>
  InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred )
  {
    for ( ; first!=last ; first++ ) if ( pred(*first) ) break;
    return first;
  }

【例1】对第三参数的处理例子,第三参数就是一函数名。。。

class CPerson
{
public:
CPerson(int a)
{
age = a;
}
public:
int age; // 年龄
};

struct finder_t
{
finder_t(int n) : age(n)
{ }
bool operator()(CPerson *p)
{ return (age == p->age); }
int age;
};

int main(void)
{
list<CPerson*> lst;
CPerson a(10),b(20),c(30);

lst.push_back(&a);
lst.push_back(&b);
lst.push_back(&c);

//finder_t t(30);
//cout << finder_t(51) << endl;
list<CPerson*>::iterator it = find_if(lst.begin(), lst.end(), finder_t(30)); // 查找年龄为50的人
if (it != lst.end()) // 找到了
{
cout << "Found person with age : " << (*it)->age;
}
else // 没找到
{
cout <<"is not find!"<<endl;
}
getchar();
return 0;
}

1 -- find_if的STL定义

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()为真。至此可能还有些不是很明了,下面举几个例子实际操练下的它的用法。

2 -- find_if在std::map查找时的应用

假如我们有个map对象是这么声明的:

std::map<int, std::string> my_map;
my_map.insert(std::make_pair(10, "china"));
my_map.insert(std::make_pair(20, "usa"));
my_map.insert(std::make_pair(30, "english"));
my_map.insert(std::make_pair(40, "hongkong"));

插入值后我们想得到值为”english”的这条记录,要怎样写程序呢?下面是个范例参考下:

1 #include<map>
2 #include<string>
3
4 classmap_finder
5 {
6 public:
7 map_finder(conststd::string&cmp_string):m_s_cmp_string(cmp_string){}
8 booloperator()(conststd::map<int,std::string>::value_type&pair)
9 {
10 returnpair.second==m_s_cmp_string;
11 }
12 private:
13 conststd::string&m_s_cmp_string;
14 };
15
16 intmain()
17 {
18 std::map<int,std::string>my_map;
19 my_map.insert(std::make_pair(10,"china"));
20 my_map.insert(std::make_pair(20,"usa"));
21 my_map.insert(std::make_pair(30,"english"));
22 my_map.insert(std::make_pair(40,"hongkong"));
23
24 std::map<int,std::string>::iteratorit=my_map.end();
25 it=std::find_if(my_map.begin(),my_map.end(),map_finder("english"));
26 if(it==my_map.end())
27 printf("notfound\n");
28 else
29 printf("foundkey:%dvalue:%s\n",it->first,it->second.c_str());
30
31 return0;
32 }

class map_finder即用于比较的函数对象,它的核心就是重载的()运算符。因为每个容器迭代器的*运算符得到的结果都是该容器的value_type值,所以该运算符的形参就是map迭代器指向的value_type类型的引用。而map的value_type到底是什么类型,就得看下STL的源代码是如何定义的。

template <class Key, class T, class Compare = less<Key>, class Alloc = alloc>
class map
{
public:
typedef Key key_type;
typedef pair<const Key, T> value_type;
......
};

从上面的定义可以看出,map的value_type是std::pair<const Key, t>类型,它的first值就是关键字,second值保存map的值域。

3 -- find_if在std::vector的应用

vector的find_if用法与map的很相似,区别仅仅是二者的value_type不一样而已。我们看下vecotr对value_type的定义。

template <class T, class Alloc = alloc>
class vector
{
public:
typedef T value_type;
typedef value_type* iterator;
......
};

可以看出vector的value_type就是容器的值类型,了解了这点,我们做个vector的find_if示范。

1 #include<vector>
2 #include<string>
3
4 structvalue_t
5 {
6 inta;
7 intb;
8 };
9
10 classvector_finder
11 {
12 public:
13 vector_finder(constinta):m_i_a(a){}
14 booloperator()(conststd::vector<structvalue_t>::value_type&value)
15 {
16 returnvalue.a==m_i_a;
17 }
18 private:
19 intm_i_a;
20 };
21
22
23 intmain()
24 {
25 std::vector<structvalue_t>my_vector;
26 structvalue_tmy_value;
27
28 my_value.a=11;my_value.b=1000;
29 my_vector.push_back(my_value);
30
31 my_value.a=12;my_value.b=1000;
32 my_vector.push_back(my_value);
33
34 my_value.a=13;my_value.b=1000;
35 my_vector.push_back(my_value);
36
37 my_value.a=14;my_value.b=1000;
38 my_vector.push_back(my_value);
39
40 std::vector<structvalue_t>::iteratorit=my_vector.end();
41 it=std::find_if(my_vector.begin(),my_vector.end(),vector_finder(13));
42 if(it==my_vector.end())
43 printf("notfound\n");
44 else
45 printf("foundvalue.a:%dvalue.b:%d\n",it->a,it->b);
46
47 getchar();
48 return0;
49 }

来自网络整理。。。find原型和使用方法!

人性最可怜的就是:我们总是梦想着天边的一座奇妙的玫瑰园,而不去欣赏今天就开在我们窗口的玫瑰。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。