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: std::find_if( vec.begin(), vec.end(), std::not1( std::bind2nd( std::mem_fun_ref(static_cast(&std::string::compare)), "findme" ) ) ) 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: std::find_if(vec.begin(), vec.end(), boost::bind(&X::name, _1) == "findme") 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.
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: std::find_if( vec.begin(), vec.end(), std::not1( std::bind2nd( std::mem_fun_ref(static_cast(&std::string::compare)), "findme" ) ) ); 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: std::find_if(vec.begin(), vec.end(), boost::bind(&X::name, _1) == "findme"); 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.
1 This doesn't compile! Reference to reference problem. – TimW Sep 4 '09 at 13:49 Right, not with VC++ (OK for MinGW and Comeau online).
I changed to use a different overload which shouldn't exhibit the same problem. (I guess the question is really about what to do in precence of overloads and not specifically how to find a string with find_if and string::compare - which is just stupid, as should be clear. ) – UncleBens Sep 4 '09 at 14:30 One more thing, the presence of overloads is just as much of a problem for boost::bind etc, unless there happens to be another way (such as in this case, using operator== instead of compare).
– UncleBens Sep 4 '09 at 14:33.
Select the correct overload yourself. Int (string::*compare)(const string&) const; compare = &string::compare; find_if(s.begin(), s.end(), bind2nd( mem_fun_ref(compare), string("findme"))); But then you get stuck with the reference to reference problem "Item #50 of Effective STL". And the boost.
Bind lib or boost. Lambda is the solution for that. Int (string::*compare)(const string&) const; compare = &string::compare; find_if(s.begin(), s.end(), bind(compare, _1, "findme")==0); Or find_if(s.begin(), s.end(), bind2nd(std::equal_to(), string("findme"))).
I think you will find that the syntax rapidly gets unweildy, if you are trying to call several functions together. Did you try something like this? Find_if(s.begin(), s.end(), bind2nd(mem_fun_ref(&string::compare), "findme")); Does this not match the char * overload?
The problem really lies in the expression mem_fun_ref(&string::compare). How do you pass a overloadset of functions to mem_fun_ref? – MSalters Sep 4 '09 at 13:16 Yes, I tried this, and no, it does not match.
– fuenfundachtzig Sep 6 '09 at 13:54.
If a function has overloaded functions, you can cast the function to correct signature like (void (*)(int,int))(&f). For your second question, if you use boost, you can do something like this, find_if(s.begin(), s.end(), boost::bind(std::equal_to(), boost::bind(&SomeClass::name, _1), name).
1 boost::bind overloads a few operators for convenience, so the "equal_to" par of your example can be replaced by using operator==: boost::bind(&SomeClass::name, _1) == name which is more readable – Éric Malenfant Sep 4 '09 at 13:01 I thought that was only in boost::lambda. I agree it is more readable in this case, but I was just trying to demonstrate how to use boost::bind – leiz Sep 4 '09 at 13:25 The original version of boost::bind did not have those operator overloads, but "recent" (from 1.33) versions have: boost. Org/doc/libs/1_40_0/libs/bind/bind.
Html#operators – Éric Malenfant Sep 4 '09 at 14:14.
I have a vector of strings s in which I would like to find a given string 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? 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.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.