在 <algorithm> 库的 find_if 中用作谓词的函数要求是什么?

What are the function requirements to use as the predicate in the find_if from the <algorithm> library?

我不知道我是否只是遗漏了一些明显的东西,但我似乎无法 find_if 工作。

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

bool isspace(char c)
{
    return c == ' ';
}

int main()
{
    string text = "This is the text";

    string::iterator it = find_if(text.begin(), text.end(), isspace);

    cout << *it << endl;

    return 0;
}

我看过这里的示例,http://www.cplusplus.com/reference/algorithm/find_if/,它可以编译和运行,但除了 vector -> string 之外,我看不出它和我的程序之间的区别,但我不明白为什么那会有所作为。

我知道 cctype 对 isspace 有更好的功能,但我想确保这没有把我搞砸。

我的错误:

test.cpp: In function ‘int main()’:
test.cpp:16:68: error: no matching function for call to ‘find_if(std::basic_string<char>::iterator, std::basic_string<char>::iterator, <unresolved overloaded function type>)’
     string::iterator it = find_if(text.begin(), text.end(), isspace);
                                                                    ^
test.cpp:16:68: note: candidate is:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from test.cpp:3:
/usr/include/c++/4.8/bits/stl_algo.h:4456:5: note: template<class _IIter, class _Predicate> _IIter std::find_if(_IIter, _IIter, _Predicate)
     find_if(_InputIterator __first, _InputIterator __last,
     ^
/usr/include/c++/4.8/bits/stl_algo.h:4456:5: note:   template argument deduction/substitution failed:
test.cpp:16:68: note:   couldn't deduce template parameter ‘_Predicate’
     string::iterator it = find_if(text.begin(), text.end(), isspace);
                                                                    ^

错误的关键部分是:

test.cpp:16:68: error: no matching function for call to ‘find_if(
    std::basic_string<char>::iterator, 
    std::basic_string<char>::iterator, 
    <unresolved overloaded function type>)’ // <==

未解析的重载函数类型!?那是因为你定义了:

bool isspace(char );

但已经有一个名为 isspace:

bool isspace(int );

和另一个名为 std::isspace 的人,你用 using:

template <class charT>
bool isspace(charT, const locale&);

而且模板无法知道您想要的是哪一个。所以你可以明确指定它:

string::iterator it = find_if(
    text.begin(), 
    text.end(), 
    static_cast<bool(*)(char)>(isspace)); // make sure yours gets called

或者,更简单,只需更改您的名字。

或者,最简单的方法是删除你的 停止 using namespace std;。这样一来,isspace 就可以明确地引用您最初想要使用的那个函数。