c++的long long int不够吗??错误

c++ long long int is not enough?? errors

本文关键字:long 错误 int c++ 不够      更新时间:2023-10-16

我在c++工作。我有一个包含以下数字

的字符串
std::string s= "8133522648";

我想把这个数字转换成

long long int nr;

I did: nr=atoll(s.c_str()). The result is: -456410944。如何解决这个错误?由于

编辑:

事实上我有:

const char* str="8133523648";
I have to convert it into long long int nr=8133523648

谢谢你的帮助!欣赏!

使用int64_t代替long long。它在stint .h

中定义

如果你依赖于boost,你可以使用

std::string s= "8133522648";
int64_t nr = boost::lexical_cast<int64_t, std::string>(s);

有以下更好的方法:

#include <sstream>
stringstream sstr;
sstr << "8133522648";
long long nr;
sstr >> nr;

不要使用atoll(),因为它没有在c++标准中定义。一些编译器可能实现它,而另一些则没有。另外,

std::string s = 8133522648;

并不意味着

std::string s = "8133522648";

这可能是你想要的

下面的代码运行正常:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    using namespace std;
    int main() {
       std::string s= "8133522648";
       long long int nr = atoll(s.c_str());
       cout << nr;
    }
相关文章: