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