什么是功能要求为predicate从&LT的find_if使用;算法>图书馆?算法、图书馆、功能、predicate

由网友(不乱于心)分享简介:我不知道如果我只是缺少明显的东西在这里,但我似乎无法得到find_if工作。的#include<的iostream>#包括<字符串>#包括<算法>使用名字空间std;布尔isspace为(字符C){回复C =='';}诠释的main(){文本字符串=这是文本;串:迭代它=...

我不知道如果我只是缺少明显的东西在这里,但我似乎无法得到find_if工作。

 的#include<的iostream>
#包括<字符串>
#包括<算法>

使用名字空间std;

布尔isspace为(字符C)
{
    回复C =='';
}

诠释的main()
{
    文本字符串=这是文本;

    串:迭代它= find_if(text.begin(),text.end(),isspace为);

    COUT<< *它<< ENDL;

    返回0;
}
 

我看这里的例子, http://www.cplusplus.com/参考/算法/ find_if / ,该编译和运行,但我看不到这一点,我的程序比其他载体之间的区别 - >字符串的事情,但我不明白为什么这会有所作为。

我知道cctype具有isspace为更好的功能,但我想,以确保没有被搞乱了我。

我的错误:

  TEST.CPP:在函数'诠释的main():
TEST.CPP:16:68:错误:没有匹配函数调用find_if(性病:: basic_string的<焦炭> ::迭代器的std :: basic_string的<焦炭> ::迭代器,<尚未解决的重载函数类型>)
     串:迭代它= find_if(text.begin(),text.end(),isspace为);
                                                                    ^
TEST.CPP:16:68:注:候选人是:
在文件中包括来自/usr/include/c++/4.8/algorithm:62:0,
                 从TEST.CPP:3:
/usr/include/c++/4.8/bits/stl_algo.h:4456:5:注意:模板<类_IIter,类_ 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:注:模板参数推导/置换失败:
TEST.CPP:16:68:注意:不能推断出模板参数'_ predicate
     串:迭代它= find_if(text.begin(),text.end(),isspace为);
                                                                    ^
 

解决方案

错误的关键部分是:

  TEST.CPP:16:68:错误:没有匹配函数调用'find_if(
    性病:: basic_string的<焦炭> ::迭代器,
    性病:: basic_string的<焦炭> ::迭代器,
    <尚未解决的重载函数类型>)'//< ==
 
Find X2 到手,这些应用我再也用不上了

未解决的重载函数类型!?因为你定义的:

 布尔isspace为(焦);
 

不过已经有一只叫 isspace为

 布尔isspace为(INT);
 

和另一个名为 的std :: isspace为

 模板<类图>
布尔isspace为(图上,常量语言环境和放大器;);
 

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

 字符串:迭代它= find_if(
    text.begin(),
    text.end(),
    的static_cast<布尔(*)(char)的>(isspace为)); //确保你被称为
 

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

或者,简单的,只需要删除你和停止 使用名字空间std; 。这样一来, isspace为明确地引用正是你想要的首先使用一种功能。

I can't tell if I'm just missing something obvious here but I cannot seem to get find_if to work.

#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;
}

I've looked at the example here, http://www.cplusplus.com/reference/algorithm/find_if/, which compiles and runs but I cannot see the difference between that and my program other than the vector -> string thing but I don't see why that would make a difference.

I know cctype has the better functions for isspace but I wanted to make sure that wasn't messing me up.

My error:

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);
                                                                    ^

解决方案

The key part of the error is:

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>)’ // <==

Unresolved overloaded function type!? That's because you defined:

bool isspace(char );

But there is already one named isspace:

bool isspace(int );

and another named std::isspace that you bring in with using:

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

And the template can't know which one it is you want. So you can either specify it explicitly:

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

Or, simpler, just change your name.

Or, simplest, just delete yours and stop using namespace std;. That way, isspace unambiguously references exactly the one function you wanted to use in the first place.

阅读全文

相关推荐

最新文章