我有以下代码:
#include <cctype>
#include <algorithm>
int main()
{
std::string str("ABCD");
std::all_of(str.begin(), str.end(), ::isxdigit);
return 0;
}
std::all_of
需要谓词作为最后一个参数。我要传递的是std::isxdigit
。当我将其作为::isxdigit
传递时,可以正常工作,但是当我将其与std::isxdigit
一起传递给 std 时,我得到了错误:12:58: error: no matching function for call to 'all_of(std::basic_string<char>::iterator, std::basic_string<char>::iterator, <unresolved overloaded function type>)'
12:58: note: candidate is:
In file included from /usr/include/c++/4.9/algorithm:62:0,
from 6:
/usr/include/c++/4.9/bits/stl_algo.h:508:5: note: template<class _IIter, class _Predicate> bool std::all_of(_IIter, _IIter, _Predicate)
all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
^
/usr/include/c++/4.9/bits/stl_algo.h:508:5: note: template argument deduction/substitution failed:
12:58: note: couldn't deduce template parameter '_Predicate'
为什么会出现此错误?
如果它确实是std,用 std 前缀传递它有什么问题?
最佳答案
为了使其与std::isxdigit
一起使用,您应该编写类似以下内容的代码:
#include <cctype>
#include <string>
#include <algorithm>
int main()
{
std::string str("ABCD");
std::all_of(str.begin(), str.end(), [](unsigned char c){ return std::isxdigit(c); });
return 0;
}
Demo
关于c++ - 提供Predicate参数时为什么不能传递 “std”前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60276348/