我如何使用find_if_not或find_if与isalpha/isspace以及C ++中的其他<cctype>函数?

问题描述 投票:0回答:2
this

,因此我尝试使用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
c++ string c++11 find clang
2个回答
1
投票
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

引起比解决的问题更多的问题。 live示例

0
投票
您可以在错误消息的第二部分中看到此问题:

isspace

“无法推断模板参数”(第三个参数的类型传递给
isspace
,也就是
using namespace std;
)。
编译器无法解决过载,因此无法弄清楚应该是哪种类型。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.