使用find_if和isalnum在字符串中查找字母数字字符

gsi*_*011 0 c++ string stl g++

我正在使用g ++ 4.7.

我想要做的就是这个,

find_if(s.begin(), s.end(), isalnum);
Run Code Online (Sandbox Code Playgroud)

其中isalnum在所定义cctype并且s是一个字符串.

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>)’
Run Code Online (Sandbox Code Playgroud)

但是,这有效,

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

find_if(s.begin(), s.end(), my_isalnum);
Run Code Online (Sandbox Code Playgroud)

如何在不创建自己的功能的情况下使其工作?

Ben*_*ley 8

编译器在此函数此函数之间消除歧义时遇到问题.你想要第一个,你必须通过使用强制转换指定签名来帮助编译器:

find_if(s.begin(), s.end(), (int(*)(int))isalnum);
Run Code Online (Sandbox Code Playgroud)

  • 请注意,添加范围解析运算符`:: isalnum`也有效. (5认同)