C++ 向量 max_size();

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3813124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:48:52  来源:igfitidea点击:

C++ vector max_size();

c++vectormax-size

提问by Kundan

On 32 bit System.

在 32 位系统上。

  1. std::vector<char>::max_size()returns 232-1, size of char— 1 byte
  2. std::vector<int>::max_size()returns 230-1, size of int— 4 byte
  3. std::vector<double>::max_size()returns 229-1, size of double— 8 byte
  1. std::vector<char>::max_size()返回 2 32-1,大小为char— 1 个字节
  2. std::vector<int>::max_size()返回 2 30-1,大小为int— 4 字节
  3. std::vector<double>::max_size()返回 2 29-1,大小为double— 8 字节

can anyone tell me max_size()depends on what?

谁能告诉我max_size()取决于什么?

and what will be the return value of max_size()if it runs on 64 bit system.

max_size()如果它在 64 位系统上运行,返回值是什么。

采纳答案by rahul sinha

Simply get the answer by

只需通过以下方式获得答案

std::vector<dataType> v;
std::cout << v.max_size();

Or we can get the answer by (2^nativePointerBitWidth)/sizeof(dataType) - 1. For example, on a 64 bit system, long longis (typically) 8 bytes wide, so we have (2^64)/8 - 1 == 2305843009213693951.

或者我们可以通过 得到答案(2^nativePointerBitWidth)/sizeof(dataType) - 1。例如,在 64 位系统上,long long(通常)是 8 字节宽,所以我们有(2^64)/8 - 1 == 2305843009213693951.

回答by Anthony Williams

max_size()is the theoretical maximum number of items that could be put in your vector. On a 32-bit system, you could in theory allocate 4Gb == 2^32 which is 2^32 charvalues, 2^30 intvalues or 2^29 doublevalues. It would appear that your implementation is using that value, but subtracting 1.

max_size()是可以放入向量的理论最大项目数。在 32 位系统上,理论上您可以分配 4Gb == 2^32,即 2^32 个char值、2^30 个int值或 2^29 个double值。看起来您的实现正在使用该值,但减去 1。

Of course, you could never really allocate a vector that big on a 32-bit system; you'll run out of memory long before then.

当然,您永远不可能在 32 位系统上真正分配这么大的向量;在那之前你就会耗尽内存。

There is no requirement on what value max_size()returns other than that you cannot allocate a vector bigger than that. On a 64-bit system it might return 2^64-1 for char, or it might return a smaller value because the system only has a limited memory space. 64-bit PCs are often limited to a 48-bit address space anyway.

除了max_size()不能分配大于该值的向量之外,对返回什么值没有要求。在 64 位系统上,它可能会为 返回 2^64-1 char,或者它可能会返回较小的值,因为系统只有有限的内存空间。无论如何,64 位 PC 通常仅限于 48 位地址空间。

回答by Vladimir

max_size()returns

max_size()返回

the maximum potential size the vector could reach due to system or library implementation limitations.

由于系统或库实施限制,向量可能达到的最大潜在大小。

so I suppose that the maximum value is implementation dependent. On my machine the following code

所以我认为最大值取决于实现。在我的机器上有以下代码

std::vector<int> v;
cout << v.max_size();

produces output:

产生输出:

4611686018427387903 // built as 64-bit target
1073741823 // built as 32-bit target

so the formula 2^(64-size(type))-1 looks correct for that case as well.

所以公式 2^(64-size(type))-1 对于这种情况看起来也是正确的。