I want to make sure a string has at least one alpha. Simple:
if ( find_if(field_name.begin(), field_name.end(), isalpha) == field_name.end() )
But I want to use a locale. I know I can easily write a separate function but I’d prefer to use it within the find_if. I.e.,
include <locale>
std::locale loc;
if ( find_if(field_name.begin(), field_name.end(), isalpha(*this_iterator,loc) == field_name.end() )
Question: Is there someway to make this_iterator refer to the then-current iterator?
In C++11 you can do this with a lambda as Timo suggests, or with
std::bind(), as instd::bind(isalpha, std::placeholders::_1, loc).Pre-C++11, you can use
std::bind2nd()instead. This gets a bit complicated though, as it requires aunary_functionorbinary_functionas an argument, instead of any old function object. We can create one usingstd::ptr_fun(), although for some reason we need to explicitly tell it what the template parameters are. And we need to usestd::isalpha()instead ofisalpha()in order to get the locale-enabled version. So the full expression looks likeNeedless to say, the C++11 version is vastly simpler.
BTW if you are using C++11 then you can use
std::any_of(...)instead ofstd::find_if(...) == foo.end(). It should behave the same, but be slightly more readable.