[英]How to work with integer limits in C++. The number "-91283472332" is out of the range of a 32-bit signed integer
**代碼中的一些問題。 我必須將字符串轉換為 integer。但有限制,32 位簽名 integer。我使用了 `stoi() function,並且不記得字符串中的空格,但是使用大數字是不可能的。 ** 我的代碼:
#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;
}
借助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.