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

发布于 2024-11-27 14:29:07 字数 390 浏览 3 评论 0原文

我正在使用 c++ 工作。我有一个包含以下数字的字符串

std::string s= "8133522648";

,我想转换该数字

long long int nr;

nr=atoll(s.c_str())。 结果是:-456410944 。如何解决这个错误?谢谢

编辑:

事实上我有:

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

感谢您的帮助!欣赏!

I am working in c++. I have a string that contains the following number

std::string s= "8133522648";

I want to convert this number in

long long int nr;

I did: nr=atoll(s.c_str()). The result is: -456410944. How to solve this error? Thanks

Edit:

In fact I have:

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

Thanks for help! Appreciate!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

永言不败 2024-12-04 14:29:07

使用 int64_t 而不是 long long。这是在 stdint.h 中定义的

如果你依赖 boost 你可以使用

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

use int64_t instead of long long. which is defined in stdint.h

If you rely on boost you can use

std::string s= "8133522648";
int64_t nr = boost::lexical_cast<int64_t, std::string>(s);
转瞬即逝 2024-12-04 14:29:07

可以通过以下更好的方式完成:

#include <sstream>

stringstream sstr;
sstr << "8133522648";

long long nr;
sstr >> nr;

不要使用atoll(),因为它不是由 C++ 标准定义的。有些编译器可能会实现它,而其他编译器则不会。另外,

std::string s = 8133522648;

并不意味着

std::string s = "8133522648";

这可能就是您想要的。

It can be done in better way as follows:

#include <sstream>

stringstream sstr;
sstr << "8133522648";

long long nr;
sstr >> nr;

Don't use atoll() as it is not defined by C++ standard. Some compiler may implement it while others don't. Also,

std::string s = 8133522648;

doesn't mean

std::string s = "8133522648";

which was probably what you wanted.

梦毁影碎の 2024-12-04 14:29:07

下面的代码工作正常:

    #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;
    }

Below code is working fine:

    #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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文