std::find是用来查找容器元素算法,但是它只能查找容器元素为基本数据类型,如果想要查找类类型,应该使用find_if.
小例子:
#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
原文:http://www.cnblogs.com/jiayayao/p/6246432.html