函数来检查字符串是否包含数字
Function to check if string contains a number
我正在用c++做一个项目(我刚刚开始学习),不明白为什么这个函数不起作用。我试图用变量first_name编写一个"Person"类,并使用函数set_first_name来设置名称。Set_first_name需要调用一个函数(下面的函数)来检查名称中是否有数字。该函数总是返回false,我想知道为什么?此外,这是检查数字的最佳方法,还是有更好的方法?
bool Person::contains_number(std::string c){ // checks if a string contains a number
if (c.find('0') == std::string::npos || c.find('1') == std::string::npos || c.find('2') == std::string::npos || c.find('3') == std::string::npos
|| c.find('4') == std::string::npos || c.find('5') == std::string::npos || c.find('6') == std::string::npos || c.find('7') == std::string::npos
|| c.find('8') == std::string::npos || c.find('9') == std::string::npos){// checks if it contains number
return false;
}
return true;
}
将所有||
更改为&&
。
更好:
return std::find_if(s.begin(), s.end(), ::isdigit) != s.end();
或者,如果你有:
return std::any_of(s.begin(), s.end(), ::isdigit);
C++11:
#include <algorithm>
#include <cctype>
#include <string>
#include <iostream>
bool has_any_digits(const std::string& s)
{
return std::any_of(s.begin(), s.end(), ::isdigit);
}
int main()
{
std::string query("H311o, W0r1d!");
std::cout << query << ": has digits: "
<< std::boolalpha
<< has_any_digits(query)
<< std::endl;
return 1;
}
输出:
H311o, W0r1d!: has digits: true
它总是返回false
,因为您的逻辑是向后的。您正在将||
运算符与== npos
检查一起使用。如果字符串中缺少任何一个特定数字,则== npos
的计算结果为true
,并且满足||
,因此返回false
。您需要使用!= npos
检查,然后如果任何检查评估为true
:,则返回true
bool Person::contains_number(const std::string &c)
{
if (c.find('0') != std::string::npos ||
c.find('1') != std::string::npos ||
c.find('2') != std::string::npos ||
c.find('3') != std::string::npos ||
c.find('4') != std::string::npos ||
c.find('5') != std::string::npos ||
c.find('6') != std::string::npos ||
c.find('7') != std::string::npos ||
c.find('8') != std::string::npos ||
c.find('9') != std::string::npos)
{
return true;
}
return false;
}
或者:
bool Person::contains_number(const std::string &c)
{
return (
c.find('0') != std::string::npos ||
c.find('1') != std::string::npos ||
c.find('2') != std::string::npos ||
c.find('3') != std::string::npos ||
c.find('4') != std::string::npos ||
c.find('5') != std::string::npos ||
c.find('6') != std::string::npos ||
c.find('7') != std::string::npos ||
c.find('8') != std::string::npos ||
c.find('9') != std::string::npos
);
}
一个更简单的解决方案是使用find_first_of()
而不是find()
:
bool Person::contains_number(const std::string &c)
{
return (c.find_first_of("0123456789") != std::string::npos);
}
如何在C++中测试字符串是否包含任何数字
这应该做到!
if (std::string::npos != s.find_first_of("0123456789"))
{
std::cout << "digit(s)found!" << std::endl;
}
您正在使用||
(或运算符)检查if语句中的几个条件。如果其中一个表达式为true,则or运算符返回true(满足条件)。
or运算符首先计算左侧的表达式:如果为true,则不计算右侧的表达式并返回true。如果左边的表达式为false,则对右边的表达式进行求值,并将其结果作为||运算符的结果返回
这就是你的功能中发生的事情:
- c是否包含"0"?如果否(因为find()中的std::string::npos表示未找到),则返回false
- c包含"1"吗?如果否,则返回false
因此,用&;(和操作员)。
相关文章:
- 如何从也包含C++字母的文本文件中提取某些数字?
- 如何确定integer_sequence在编译时是否包含给定的数字?
- 如何将较大数字中从0到9的单个数字放入只包含数字的数组中
- 如何识别字符串只包含数字
- 如何在C++中创建 2d 数组,其中一部分包含循环计数器,另一部分包含数字列表?
- 如何对包含数字和符号的字符串进行排序?
- 如何在包含数字的.bmp(二进制图像)中执行组件标记9 4 3 6
- 分类包含数字和单词的字符串向量
- 如何检查整数是否包含数字 0 和 1
- 求所有和中最大的和,它包含数字和
- 用于确定 2x2 正方形是否包含数字 1-4(无重复)的算法
- 将字符串(包含数字)转换为整数并返回该整数
- 函数来检查字符串是否包含数字
- 对包含数字的字符串进行排序
- 将包含数字的字符串解析为整数数组
- 在检测字符串是否包含数字字符时,这有什么问题
- 用于查找包含数字的非重叠范围的有效数据结构
- 福尔。while 循环:我应该怎么做,使输入 (int) 不包含数字 '1' 或"0"?
- c++递归检查number是否包含数字
- 将包含数字和单词的文本文件列表转换为整数2D数组