c++ - isdigit() 无法将 "1"识别为数值

标签 c++ validation

我正在解析一个字符串,验证字符串中的数字是否为 int。

std::string segmentS
if(!isdigit(std::stoi(segmentS))){
    std::cout<<"Not a location +"<<segmentS<<std::endl;

    //does something
    break;
}

segmentS 是假设为整数的子串 即使我确保 segmentS 是一个数字,!isdigit(std::stoi(segmentS)) 仍然成立。即使在 not a location 消息之后打印出 segmentS,它也是一个数字 1,但是当它遇到此 if 语句时,它不会被视为数字。

最佳答案

std::stoi(segmentS) 会将 segmentS 转换为 int 值,然后传递给 isdigit,除了 isdigit 假定输入是一个字符

删除对 stoi 的调用。

关于c++ - isdigit() 无法将 "1"识别为数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34258864/

相关文章:

php - 为什么我的页面不能在 Internet Explorer 中正确显示?

validation - Angular2/ Material 2 : md-input-container label is not resetting float when value is changed programmatically

c++ - 位域内存

c++ - std::for_each,调用带有引用参数的成员函数

c++ - 基本 C++ : for-each loop

c++ - 将包含 initializer_list 的参数包扩展到构造函数

Scala:将整个列表的 Either 与每个元素的 Either 结合起来

javascript - 正则表达式不允许在字符串开始或结束处出现 '.' 、 '_' 、 '-' ,并且不应有连续的 '.' ,其余所有特殊字符都不应被允许

java - 带有属性或参数的 Mule 自定义验证

c++ - 为什么不捕获 `std::promise::~promise` 中已经传播的异常