gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:34:52 25 4
gpt4 key购买 nike

我正在解析一个字符串,验证字符串中的数字是否为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com