C++ 在<;中的find_if中用作谓词的功能要求是什么;算法>;图书馆?

C++ 在<;中的find_if中用作谓词的功能要求是什么;算法>;图书馆?,c++,algorithm,C++,Algorithm,我不知道我是否错过了一些明显的东西,但我似乎无法找到工作 #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 i

我不知道我是否错过了一些明显的东西,但我似乎无法找到工作

#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;
}
#包括
#包括
#include,它编译并运行,但我看不出它和我的程序之间的区别,除了vector->string的东西,但我不明白为什么会有区别

我知道cctype对iSpace有更好的功能,但我想确保这不会把我搞砸

我的错误:

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:在函数“int main()”中:
test.cpp:16:68:错误:调用“find_if(std::basic_string::iterator,std::basic_string::iterator,)没有匹配的函数”
string::iterator it=find_if(text.begin(),text.end(),isspace);
^
测试。cpp:16:68:注:候选人为:
在/usr/include/c++/4.8/algorithm:62:0中包含的文件中,
来自测试。cpp:3:
/usr/include/c++/4.8/bits/stl_-algo.h:4456:5:注意:模板_-IIter std::find_-if(_-IIter,_-IIter,_-Predicate)
查找\u if(\u InputIterator\uuuu优先,\u InputIterator\uuu最后,
^
/usr/include/c++/4.8/bits/stl_algo.h:4456:5:注意:模板参数推导/替换失败:
test.cpp:16:68:注意:无法推断模板参数“\u谓词”
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>)’ // <==
但已经有一个名为:

还有另一个名字是你用
带来的

template <class charT>
bool isspace(charT, const locale&);
模板
bool-isspace(图表、const-locale&);
并且模板无法知道您想要哪一个。因此您可以显式指定它:

string::iterator it = find_if(
    text.begin(), 
    text.end(), 
    static_cast<bool(*)(char)>(isspace)); // make sure yours gets called
string::iterator it=find\u if(
text.begin(),
text.end(),
static_cast(isspace));//确保调用您的
或者,更简单一点,只需更改您的姓名


或者,最简单的方法是删除您的,然后使用namespace std;
停止
。这样,
isspace
就明确地引用了您首先要使用的函数。

尝试
查找if(text.begin(),text.end(),::isspace)
(注意
isspace
之前的额外
)@Oktalist:仅用
限定它是不够的。OP需要将它强制转换为
(bool(*)(char))因此,编译器可以解决使用哪个<代码> iSuth。C++标题可能包含“代码> <代码>,在这种情况下,全局命名空间中有2个代码> iStase函数。
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