Hello,
First, find_if() is not a functor. A functor is an object that act like a function. To be able to do so, it has to override the operator(), like in this example:
struct MyComparisonFunctor{ bool operator()(int param1, int param2) { return param1 < param2; }};
Calling this code is trivial:
MyComparisonFunctor functor;if (functor(1, 2)) { std::cout << "1 < 2!!!!11 Incredebal!!!11" << std::endl;}
std::find_if uses a predicate to perform the comparison. A predicate is either a functor or a function pointer. A correct way to use find_if is to define a function like this:
std::vector<int> v;bool find_my_key(int k){ if (k == 32) return true; return false;}// use find_if:std::vector<int>::iterator iter = std::find_if(v.begin(), v.end(), find_my_key);
Another way is to define a functor. In this case, it is considered a good practice to inherit this functor from either std::unary_function or std::binary_function (depending on your needs, of course):
#include <functional>struct FindKey : public std::unary_function<int, bool>{ FinKey(const argument_type& key_to_find) : mKeyToFind(key_to_find) { } result_type operator() (const argument_type& value) { return (result_type) (value == mKeyToFind); }private: argument_type mKeyToFind;}// use find_if:FindKey finder(32);std::vector<int>::iterator iter = std::find_if(v.begin(), v.end(), finder);
I'd like to add one more thing: if you want to find if an object exists in the map I suggest you to use map<>::find() instead of std::find_if(). The former might be optimized to find your key faster, while the later will force you to run a O(n) algorithm. Of course, your search need might be different (and maybe a map is not the correct container to use then).
HTH,