Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Strchr函数能否在c++;?_C++_Strchr - Fatal编程技术网

C++ Strchr函数能否在c++;?

C++ Strchr函数能否在c++;?,c++,strchr,C++,Strchr,是否可以在字符串中找到多个字符,并使用strchr? 我试过上面的方法,但不起作用。我想这是因为strchr返回字符的出现次数 附言:。我试过了 Diff FBullAndCow::EDifficulty(std::string diff) const { if ((diff.length() > 1)) { return Diff::Not_Number; } else if (!strchr(diff.c_str(), '3' || '4

是否可以在字符串中找到多个字符,并使用strchr? 我试过上面的方法,但不起作用。我想这是因为strchr返回字符的出现次数

附言:。我试过了

Diff FBullAndCow::EDifficulty(std::string diff) const
{
    if ((diff.length() > 1))
    {
        return Diff::Not_Number;
    }
    else if (!strchr(diff.c_str(), '3' || '4' || '5' || '6' || '7' || '8'))
    {
        return Diff::Not_Number;
    }
    return Diff::Ok;
}
用这种方式,虽然它可能是愚蠢的。我完全是个新手。。。我花了几个小时试图寻找一条路,但因为我什么也找不到,我就在这里


编辑:它需要返回找到的数字。很抱歉省略此项。

直接回答是:不,您不能在
strchr
中检查多个字符。该函数只查找一个特定的字符

如果您需要搜索所有数字字符,因为您使用的是
std::string
(为什么要使用别名?)。或者,更可能的是,检查
diff.find\u first\u not\u of(“0123456789”)==std::string::npos


然而,即使这样也不是一个好的解决方案——因为假设一旦你确认它是数字,你就会想要实际的数字。因此,使用并验证它没有抛出并使用整个字符串可能更直接。

或者,我可以编写六个不同的if语句,这是可行的,但这相当难看……什么是
FString
?什么是
Diff
?您跳过
0
1
2
9
的任何原因?@Barry FString>>使用FString=std::string;Diff是一个枚举类。但这在这里并不重要。而且,我没有特别的理由跳过其余的数字。仅举一些例子。不要使用strchr,查看std::string::find_first_of,std::string::find_first_not_of注意
'3'| | |'4'
等的值为1
|
是一个布尔运算符;如果两个操作数都是
false
,则其结果为
false
,否则为
true
。检查此函数后,它似乎完成了我需要的操作,尽管我不知道它。非常感谢!假设我需要一个int输入,例如从2到9。在if语句中如何使用它?diff将是一个字符串,我需要检查它是否是2到9之间的数字之一。我知道如何检查它的长度。剩下的是什么?我想我需要一个变量,比如:
std::string num=“23456789”;if(){return Diff::Ok;//这是一个枚举值。}
假设我在Diff变量中输入了5。如何测试diff中的输入是否为这些数字之一@Barry@CorpseDead只需将其转换为
int
    if ((!strchr(diff.c_str(), '3')) || (!strchr(diff.c_str(), '4')))