string s="34567789123456";
int a=atoi(s.c_str());
// 此时 a=2147483647;因为int 型数据存储范围是 -2147483648 ~ 2147483647
// 解决方法
typedef long long int cint; // 自定义 高精度长整形cint
cint a=cint(atof(s.c_str());
也可能是 atoi(string)=-2147483647;解决方法同上。
博客探讨了在C++中将字符串转换为整数时遇到的溢出问题,当转换的数值超出int类型范围时,结果会变成最大值2147483647。为了解决这个问题,提出了使用长整型(long long int)作为数据类型进行转换的方法,以确保能够存储更大范围的数值,避免溢出错误。
string s="34567789123456";
int a=atoi(s.c_str());
// 此时 a=2147483647;因为int 型数据存储范围是 -2147483648 ~ 2147483647
// 解决方法
typedef long long int cint; // 自定义 高精度长整形cint
cint a=cint(atof(s.c_str());
也可能是 atoi(string)=-2147483647;解决方法同上。
2万+
3558

被折叠的 条评论
为什么被折叠?
