问题描述
我对此有点困惑,这两者在我看来是一样的.尽管可能会发生容量和大小在不同编译器上有所不同的情况.它可能如何不同.它还说,如果我们的内存不足,容量会发生变化.
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 是向量的总空间量.在引擎盖下,向量只是使用一个数组.向量的容量是该数组的大小.这始终等于或大于大小.它们之间的区别在于在需要重新分配引擎盖下的数组之前可以添加到向量中的元素数量.
Capacity is the amount of total space that the vector has. 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.
这篇关于向量的大小与容量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!