,因此我尝试使用find_if_not
编译,并在
--std=c++11
中定义,并在命名空间<algorithm>
。
我有以下代码:std
这个想法是简单地呼应输入,但没有任何领先的非alphanumeric字符。但是,我收到一个编译器错误:
#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main(int argc, const char *argv[]) {
string buffer;
getline(cin, buffer);
cout << buffer.substr(find_if(buffer.begin(), buffer.end(), isalnum) - buffer.begin()) << endl;
return 0;
}
然后,我尝试使用error: no matching function for
call to 'find_if'
cout << buffer.substr(find_if(buffer.begin(), buffer.end(), isal...
^~~~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:930:1: note:
candidate template ignored: couldn't infer template argument '_Predicate'
find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
^
1 error generated.
我如何使用这些功能find_if_not(buffer.begin(), buffer.end(), isspace)
和
find_if
?我尝试使用所有必需的标头和C ++版本。
当我跑步时
find_if_not
我得到:
g++ --version
在
COMMENT中建议,您不能直接在
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
函数中使用<cctype>
函数。那些期望接受
find_if
find_if_not
的回调,而c函数unsigned char
和
bool
且类似地接受isalpha
并返回isspace
。将功能指针转换为另一种类型,然后调用无效。
解决方案是通过正确签名的lambda接口这些功能:
int
或创建正确的签名的新功能,然后通过:
int
首先,我认为我的问题是由于功能
cout << buffer.substr(buffer.begin() - find_if_not(buffer.begin(), buffer.end(),
[](unsigned char c) -> bool {return isspace(c);})) << endl;
和
bool is_space(unsigned char c) {
return isspace(c);
}
没有以某种方式定义。
以后参考,如果我得到:
find_if
意味着我的论点不正确。如果我得到:
find_if_not
这意味着我需要检查拼写,包括,C ++版本等。
您的问题原来是
error: no matching function for call
轻快地删除它将两个不同的函数引入查找中,包括。当您使用时,它通常使用过载分辨率规则选择,但在模板扣除时间无法弄清楚,因此您会遇到错误。
error: use of undeclared identifier
using namespace std;
作为一般规则,isspace
std::isspace
isspace
“无法推断模板参数”(第三个参数的类型传递给
isspace
,也就是
using namespace std;
)。编译器无法解决过载,因此无法弄清楚应该是哪种类型。