如何避免字符串到整数转换情况下的无效参数异常

How to avoid invalid argument exception in case of string to integer conversion

本文关键字:无效 参数 异常 情况下 转换 何避免 字符串 整数      更新时间:2023-10-16

在C#中,有一些try函数可以避免异常,例如Int.TryParse((。我正在寻找类似的C++函数作为 std::stoi(( 的替代品。

C++的方式:

unsigned long value;
std::istringstream s(data);
if(s >> value)
{
     // possibly check, if the stream was consumed entirely...
}

我喜欢这种方法的地方:如果使用cstdint的类型,则无需担心是否使用具有正确函数的正确类型...