![](/img/trans.png)
[英]How to check if a string contains all of the characters of another string?
[英]Check if all characters in a string are 1, 0, or “ ”
我正在尝试创建一个布尔函数,如果字符串仅包含1、0和空格,则该函数返回false。 否则,它会返回true到嵌套函数调用的while循环。这是我到目前为止的代码:
bool check_bin_num(string& bin) {
for (int i = 0; i <= back.length(); i++) {
if (!(bin[i] == '0' || bin[i] == '1' || bin[i] == ' ')) {
return true;
}
}
return false;
}
任何帮助,将不胜感激!
可以使用std::string::find_first_not_of
在一行中std::string::find_first_not_of
:
s.find_first_not_of("01 ") == std::string::npos
当且仅当std::string
s
完全由字符'0'
, '1'
和' '
(空格)组成时,才会计算为true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.