stl::find_if用法总结

自己学习的笔记,转载于http://www.cnblogs.com/motadou/archive/2009/02/01/1561549.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)
    {
         for(; first != last ; first++)
              if(pred(*first)) break;
          return first;
    }
它在区间[first,end)中搜寻使一元判断式pred为true的第一个元素。如果没有找到,返回end。
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)
           {
                return pair.second == m_s_cmp_string;
           }
    private:
            const std::string &m_s_cmp_string;                    
    };

    int main()
    {
        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"));    

        std::map<int, std::string>::iterator it = my_map.end();
        it = std::find_if(my_map.begin(), my_map.end(), map_finder("english"));
        if (it == my_map.end())
           printf("not found\n");       
        else
           printf("found key:%d value:%s\n", it->first, it->second.c_str());

        return 0;        
    }
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;
    };

    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;        
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值