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