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
C++ vector max_size();
提问by Kundan
On 32 bit System.
在 32 位系统上。
std::vector<char>::max_size()
returns 232-1, size ofchar
— 1 bytestd::vector<int>::max_size()
returns 230-1, size ofint
— 4 bytestd::vector<double>::max_size()
returns 229-1, size ofdouble
— 8 byte
std::vector<char>::max_size()
返回 2 32-1,大小为char
— 1 个字节std::vector<int>::max_size()
返回 2 30-1,大小为int
— 4 字节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 long
is (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 char
values, 2^30 int
values or 2^29 double
values. 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
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 对于这种情况看起来也是正确的。