容器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”的这条记录,要怎样写程序呢?下面是个范例参考下:

#include <map>
#include <string>
 
class map_finder
{
public:
       map_finder(const std::string &cmp_string):m_s_cmp_string(cmp_string){}
       bool operator ()(const std::map<int, std::string>::value_type &pair)
       {
10              return pair.second == m_s_cmp_string;
11         }
12  private:
13          const std::string &m_s_cmp_string;                    
14  };
15   
16  int main()
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>::iterator it = 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("not found\n");       
28      else
29         printf("found key:%d value:%s\n", it->first, it->second.c_str());
30         
31      return 0;        
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示范。

#include <vector>
#include <string>
 
struct value_t
{
    int a;
    int b;
};
 
10  class vector_finder
11  {
12  public:
13      vector_finder(const int a):m_i_a(a){}
14      bool operator ()(const std::vector<struct value_t>::value_type &value)
15      {
16          return value.a == m_i_a;
17      }
18  private:
19      int m_i_a;                    
20  };
21   
22   
23  int main()
24  {
25      std::vector<struct value_t> my_vector;
26      struct value_t my_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<struct value_t>::iterator it = 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("not found\n");       
44      else
45         printf("found value.a:%d value.b:%d\n", it->a, it->b);
46         
47      getchar();
48      return 0;        
49  }

  

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

世界如此的美好,江山如此的多娇! ---阳光正能量--->>>>>>>>>>>>>>>
原文地址:https://www.cnblogs.com/upendi/p/2588358.html