This may seem to be an academic question, but still I would be very interested in the answer:
I have a vector of strings s in which I would like to find a given string findme. This can be done using something like
find(s.begin(), s.end(), findme);
My question is: There must be a way doing the same using find_if and the compare method of the STL strings as predicate, but how? Something like
find_if(s.begin(), s.end(), bind2nd(mem_fun_ref(&string::compare), string("findme")) );
does not work, because the compare method has several overloads and the compiler does not know which one to choose.
As a second step: My motivation for using find_if instead of find is that I have a vector of objects derived from a class having a string property name and I want to find an object with a given name. Is this possible (without writing an extra function to be used as predicate)?
EDIT: As some (most 🙂 answers mentioned using Boost — I would prefer not to have to include Boost for this. (As far as I know, most of the Boost libraries are “only” templates, so there should be a way without using Boost.)
One option is to cast the member function pointer to suitable type. Another thing you are forgetting is that std::string::compare returns 0 for equal strings, so you’ll also need to negate the functor. All in all:
As to your rationale against boost: its templates are an order of a magnitude more flexible than what you can find in STL functional header. It’s either boost, you wait for C++0x lambdas (which I believe will be the preferable way in such situations) or you write some helpers yourself. Currently it can’t get simpler than:
FYI, C++0x will add std::bind which is similar to boost::bind but it seems the convenience of overloaded operator== will not be there.