![](/img/trans.png)
[英]In C++ STL, how do I remove non-numeric characters from std::string with regex_replace?
[英]std::stoi - String with non-numeric characters getting parsed as an integer without throwing exception (c++)
当使用std :: stoi并传递以数字开头且后跟非数字字符的字符串时,该字符串将成功解析为整数,而不会引发异常。 例如,“ 0abcf”被解析为0。仅当字符串仅包含数字字符(即“ 123”但不包含“ 12a”)时,我希望将字符串解析为整数,是否存在现有功能呢?
引用文档 :
int stoi( const std::string& str, std::size_t* pos = 0, int base = 10 );
...
[第一个未转换的字符]的索引将被计算并存储在*pos
,给出转换后处理的字符数。
因此,要检查是否所有字符都有效/已解析,您需要做的就是传递第二个参数,然后检查此值(转换后的字符数)是否等于字符串中的字符数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.