stl“向量< T>太长“ [英] stl "vector<T> too long"
问题描述
我在其他答案中读到,没有c ++编译器强加的限制 std :: vector的最大大小。我试图使用向量为一个目的,并需要有10 ^ 19项。
i read in other answers that theres no limit imposed by c++ compiler maximum size of std::vector. i am trying to use vector for one purpose, and in need to have 10^19 items.
typedef struct{
unsigned long price, weight;
}product;
//inside main
unsigned long long n = 930033404565174954;
vector<product> psorted(n);
程序在最后一条语句中断开。如果我尝试 resize(n)
而不是用 n
初始化,然后也有程序中断与消息:
the program breaks on the last statement. if i try resize(n)
instead of initializing with n
then also program breaks with message :
vector<T> too long
std::length_error at memory location
我需要对数据进行排序价格后放入矢量。我应该怎么办?
i need to sort the data accourding to price after putting in vector. what should i do ?
推荐答案
std :: vector
对于可以携带的东西有限制。您可以使用 std :: vector :: max_size
查询此值,它返回您可以使用的最大大小。
std::vector
does have limits on how much stuff it can carry. You can query this with std::vector::max_size
, which returns the maximum size you can use.
10 ^ 19项。
10^19 items.
你有 10 ^ 19 * sizeof产品)
内存?我猜你没有〜138 Exa 字节的RAM。此外,你必须在64位模式下编译,甚至考虑分配这么多。编译器没有打破;你的执行打破了尝试分配太多东西。
Do you have 10^19 * sizeof(product)
memory? I'm guessing that you don't have ~138 Exabytes of RAM. Plus, you'd have to be compiling in 64-bit mode to even consider allocating that much. The compiler isn't breaking; your execution is breaking for trying to allocate too much stuff.
这篇关于stl“向量< T>太长“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!