gpt4 book ai didi

c++ - 如何在 C++ 中创建大型数组(如 60 亿)?

转载 作者:太空宇宙 更新时间:2023-11-04 14:52:21 25 4
gpt4 key购买 nike

我正在尝试创建一个大尺寸数组(大约 80000*80000)

我试过了

int *bigarray = new int[80000*80000];

但它给了我错误

terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc

我想做的是创建一个社交网络的边缘矩阵。

谁能告诉我如何创建大尺寸数组或任何其他方法来解决它?

谢谢!

最佳答案

您可以通过不使用 int 来停止溢出警告, 使用 unsigned long long - 添加 ULL到你的文字数字的结尾:

auto arr = new int[80000ULL * 80000ULL];

我建议使用 std::vector而不是原始动态数组:

auto vec = std::vector<int>(80000ULL * 80000ULL);

你的 std::bad_alloc意味着你没有足够的内存....

And the thing I want to do is to create a social network's edge matrix.

您可以考虑使用“稀疏矩阵”。如果数组中的大部分位置都是空的,则可以使用仅记录已占用单元格值的数据结构。

一个粗略的例子是使用 std::map<std::size_t, std::map<std::size_t, int>>但是有一些专门的库可以做得更好。

例如 Eigen Library . ( Prop @yar)

关于c++ - 如何在 C++ 中创建大型数组(如 60 亿)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51921121/

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