Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何避免字符串到整数转换时出现无效参数异常_C#_C++ - Fatal编程技术网

C# 如何避免字符串到整数转换时出现无效参数异常

C# 如何避免字符串到整数转换时出现无效参数异常,c#,c++,C#,C++,在C#中,有一些try函数可以避免异常,例如Int.TryParse()。 我在寻找一个类似的C++函数,作为STD::StII.()的替代。 < P> C++ >类似的方式: unsigned long value; std::istringstream s(data); if(s >> value) { // possibly check, if the stream was consumed entirely... } 我喜欢这种方法:如果使用cstdint的类型,

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

< P> C++ >类似的方式:

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

我喜欢这种方法:如果使用
cstdint
的类型,您不必担心是否将正确的类型与正确的函数一起使用…

您希望能够捕获异常吗?您可以使用来包装对任何函数的调用,如果捕获异常,则返回false使用类似strtol()的函数如果你想避免例外测试:我使用C++版本的好的OC C代码> STRTO 函数族。其余的链接在页面底部,就在用法示例的下方。