STL的find_if函数

引用 http://blog.chinaunix.net/uid-20437338-id-1946550.html

有时我们要在map、vector容器中查找符合条件的记录,map提供一个find的成员函数,但也仅限于查找关键字满足条件的记录,不支持值域的比较。如果我们要在值域中查找记录,该函数就无能无力了。而vector甚至连这样的成员函数都没有提供。所以一般情况下进行值域的查找,要么自己遍历数据,要么求助于STL的find_if函数。前种方法我们这里就不赘述了,只讲find_if函数。
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   class map_finder 
5   { 
6   public: 
7          map_finder(const std::string &cmp_string):m_s_cmp_string(cmp_string){} 
8          bool operator ()(const std::map<int, std::string>::value_type &pair) 
9          { 
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示范。
1   #include <vector> 
2   #include <string> 
3     
4   struct value_t 
5   { 
6       int a; 
7       int b; 
8   }; 
9     
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   }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值