大小与载体的能力? [英] size vs capacity of a vector?

查看:162
本文介绍了大小与载体的能力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困惑,这两个看起来都和我一样。
虽然可能发生不同编译器的容量和大小可能不同。如何可能有所不同。
它还说如果我们的内存不足的容量变化。

I am a bit confused about this both of these look same to me. Although it may happen that capacity and size may differ on different compilers. how it may differ. Its also said that if we are out of memory the capacity changes.

这些东西对我来说都不清楚。

All these things are bit unclear to me.

有人可以给出解释(如果可能的话和示例,或者我可以对任何程序进行任何测试来理解它)

Can somebody give an explanation.(if possible with and example or if I can do any test on any program to understand it)

推荐答案

大小不允许在多个编译器之间有所不同。向量的大小是它包含的元素的数量,它是由您放入向量中的元素数量直接控制的。

Size is not allowed to differ between multiple compilers. The size of a vector is the number of elements that it contains, which is directly controlled by how many elements you put into the vector.

容量是向量当前正在使用的空间量。在引擎盖下,向量只是使用数组。向量的容量是该数组的大小。这总是等于或大于大小。它们之间的区别是你可以添加到向量中的元素的数量在引擎盖下的数组需要重新分配。

Capacity is the amount of space that the vector is currently using. Under the hood, a vector just uses an array. The capacity of the vector is the size of that array. This is always equal to or larger than the size. The difference between them is the number of elements that you can add to the vector before the array under the hood needs to be reallocated.

你几乎不必关心容量。它的存在,让具有非常具体的性能和内存限制的人做他们想要的。

You should almost never care about the capacity. It exists to let people with very specific performance and memory constraints do exactly what they want.

这篇关于大小与载体的能力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆