Finding an alphanumeric character in a string usin

2020-02-16 04:03发布

问题:

I'm using g++ 4.7.

What I'm trying to do is this,

find_if(s.begin(), s.end(), isalnum);

where isalnum is defined in cctype and s is a string.

logman.cpp:68:47: error: no matching function for call to ‘find_if(std::basic_string<char>::const_iterator, std::basic_string<char>::const_iterator, <unresolved overloaded function type>)’

However, this works,

bool my_isalnum(int c) {
    return isalnum(c);
}

find_if(s.begin(), s.end(), my_isalnum);

How can I get this to work without creating my own function?

回答1:

The compiler is having trouble disambiguating between this function and this function. You want the first one, and you'll have to help the compiler out here, by specifying the signature with a cast:

find_if(s.begin(), s.end(), (int(*)(int))isalnum);


回答2:

This should work.

#include <algorithm>
#include <cctype>
auto result = std::find_if (begin(s), end(s), std::isalnum);


回答3:

This should work

#include <algorithm >
#include <cctype>

auto result = std::find_if(std::begin(s), std::end(s),  isalnum) ;


标签: c++ string stl g++