我有一个简单的程序,调用std::find_if
,我想我已经通过了前两个参数作为迭代器,第三个作为预测,但代码仍然不会编译,任何想法?为什么find_if不能在我的程序中工作?
#include <string>
#include <cctype>
#include <algorithm>
bool notspace(char ch);
bool space(char ch);
int main() {
typedef std::string::const_iterator iter;
iter i;
std::string s = "ab c";
i = std::find_if(i, s.end(),space);
return 0;
}
bool space(char ch) {
return std::isspace(ch);
}
错误消息:
q-isspace.cpp: In function ‘int main()’:
q-isspace.cpp:12:38: error: no matching function for call to ‘find_if(iter&, std::__cxx11::basic_string<char>::iterator, bool (&)(char))’
i = std::find_if(i, s.end(),space);
^
In file included from /usr/include/c++/5/algorithm:62:0,
from q-isspace.cpp:3:
/usr/include/c++/5/bits/stl_algo.h:3806:5: note: candidate: template<class _IIter, class _Predicate> _IIter std::find_if(_IIter, _IIter, _Predicate)
find_if(_InputIterator __first, _InputIterator __last,
^
/usr/include/c++/5/bits/stl_algo.h:3806:5: note: template argument deduction/substitution failed:
q-isspace.cpp:12:38: note: deduced conflicting types for parameter ‘_IIter’ (‘__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >’ and ‘__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >’)
i = std::find_if(i, s.end(),space);
'i'和'j'没有被初始化为任何东西。那么'find_if'应该从哪里开始呢? – UnholySheep