STL的std::find和std::find_if


  std::find是用来查找容器元素算法,但是它只能查找容器元素为基本数据类型,如果想要查找类类型,应该使用find_if. STL算法的一个版本采用缺省的运算行为,该算法的另一个版本提供额外参数,接收外界传入的一个仿函数(functor),以便采用其他策略。可以采用其他策略的算法通常是以_if作为尾词,例如find_if(), replace_if().

  与此类似的,质变算法(改变操作区间内元素的值)分为in-place(就地计算)和copy(将对象内容拷贝一份副本,在副本上运算完毕后返回)版,copy版总是以_copy作为函数名称尾词

  小例子:

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <boost/format.hpp>  
#include <boost/cstdint.hpp>

using namespace std;
using namespace boost;


struct find_func{
    find_func(unsigned int var1)
        : var(var1) {
    }

    bool operator()(const std::map<boost::uint64_t,boost::uint64_t>::value_type &pair) {
        int var1 = pair.second;
        return (var == var1);
    }

    unsigned int var;
};

static std::map<boost::uint64_t,boost::uint64_t> test_map;

int main()
{
    std::cout<<"STD::FIND--------------------------------"<<std::endl;
    std::vector<int> v;
    for (int i = 0; i < 10; ++i)
        v.push_back(i);

    std::vector<int>::iterator iter = std::find(v.begin(), v.end(), 3);

    if (iter == v.end())
        std::cout << "Can not find value 3 in v" << std::endl;
    else
        std::cout << "The index of value " << (*iter) << " is " 
        << std::distance(v.begin(), iter) << std::endl;

    std::cout<<"STD::FIND_IF--------------------------------"<<std::endl;
    test_map[1000] = 1000;
    test_map[2000] = 2000;
    test_map[3000] = 3000;
    std::map<boost::uint64_t,boost::uint64_t>::iterator it = test_map.end();
    it = std::find_if(test_map.begin(), test_map.end(), find_func(2000));
    if (it == test_map.end()) {
        cout<<"Can not find the int variable"<<endl;
    } else {
        std::cout << "the index of value " << (*it).first << " is " 
        << std::distance(test_map.begin(), it) << std::endl;
    }

    return 0;
}
//Output:

STD::FIND---------------------------
The index of value 3 is 3
STD::FIND_IF------------------------
the index of value 2000 is 1


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM