gpt4 book ai didi

char数组中的C++ Num到int

转载 作者:行者123 更新时间:2023-11-30 05:38:35 24 4
gpt4 key购买 nike

我将大量数字存储到 char 数组中,然后将其返回到 cout 中。

#include <iostream>

int num()
{
char num[] = "160919040944";

return (int)num;
}
int main()
{
std::cout << num();

return 0;
}

它打印出一个随机数而不是我指定的那个。

最佳答案

如评论中所述,(int)num 不会这样做。你需要的是通过 std::stoll 从你的函数返回一个 long long

long long num()
{
char num[] = "160919040944";
return std::stoll(num); // need conversion to long due to overflowing an int
}

函数std::stollstd::string(在本例中从 char* 隐式构造)转换为 long long int

感谢@Lorehead 的评论

关于char数组中的C++ Num到int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32674104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com