[英]How to work with integer limits in C++. The number "-91283472332" is out of the range of a 32-bit signed integer
**Some problems in code. **代码中的一些问题。 I must convert a string to an integer. But have limited, 32-bit signed integer. I used the `stoi() function, and did not remember about spaces in string, however using big numbers are impossible.我必须将字符串转换为 integer。但有限制,32 位签名 integer。我使用了 `stoi() function,并且不记得字符串中的空格,但是使用大数字是不可能的。 ** My code: ** 我的代码:
#include <iostream>
#include <string>
#include <limits.h>
using namespace std;
int myAtoi(string str)
{
int Res = 0;
for (int i = 0; i < str.size(); i++)
{
if (str[0] >= 'a' && str[0] <= 'z')
{
return 0;
}
}
for (int i = 0; i < str.size(); i++)
{
if (str[i] == ' ')
i++;
// if(str[i]>='0' || str[i]<='9' )
//// if(str[i]==' ')
//// i++;
Res = stoi(str);
cout << "Res:" << Res << endl;
if (Res <= INT_MIN)
{
return INT_MIN;
}
if (Res >= INT_MAX)
{
return INT_MAX;
}
}
cout << "MIN=" << INT_MAX << endl;
cout << "Res=" << Res;
// return Res;
}
Get the help of stoll()
which converts a std::string
object into long long
type integer:借助stoll()
将std::string
object 转换为long long
类型 integer:
void printLongNumber(std::string str) {
auto number = stoll(str);
std::cout << number << std::endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.