C++ 向量的大小与容量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6296945/
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
size vs capacity of a vector?
提问by munish
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)
有人可以给出解释。(如果可能的话,或者我可以对任何程序进行任何测试以理解它)
回答by John Calsbeek
Sizeis 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.
多个编译器之间的大小不允许不同。向量的大小是它包含的元素数量,这直接由您放入向量中的元素数量控制。
Capacityis 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.
您几乎不应该关心容量。它的存在是为了让具有非常具体的性能和内存限制的人做他们想做的事。
回答by Kent Boogaart
Size: the number of items currently in the vector
大小:向量中当前的项目数
Capacity: how many items can be fit in the vector before it is "full". Once full, adding new items will result in a new, larger block of memory being allocated and the existing items being copied to it
容量:向量在“满”之前可以容纳多少项。一旦已满,添加新项目将导致分配一个新的更大的内存块,并将现有项目复制到其中
回答by Coeffect
Let's say you have a bucket. At most, this bucket can hold 5 gallons of water, so its capacity is 5 gallons. It may have any amount of water between 0 and 5, inclusive. The amount of water currently in the bucket is, in vector terms, its size. So if this bucket is half filled, it has a size of 2.5 gallons.
假设你有一个水桶。这个桶最多可以装 5 加仑的水,所以它的容量是 5 加仑。它可以含有介于 0 和 5 之间的任意数量的水,包括 0 和 5。桶中当前的水量,以向量的形式,就是它的大小。所以如果这个桶装了一半,它的大小是 2.5 加仑。
If you try to add more water to a bucket and it would overflow, you need to find a bigger bucket. So you get a bucket with a larger capacity and dump the old bucket's contents into the new one, then add the new water.
如果您尝试向桶中添加更多水并且会溢出,则需要找一个更大的桶。所以你得到一个容量更大的水桶,把旧水桶里的东西倒进新水桶里,然后加入新水。
Capacity: Maximum amount of stuff the Vector/bucket can hold. Size: Amount of stuff currently in the Vector/bucket.
容量:Vector/bucket 可以容纳的最大数量。大小:当前在 Vector/bucket 中的东西的数量。
回答by sanjeev
Sizeis number of elements present in a vector
大小是向量中存在的元素数
Capacityis the amount of space that the vector is currently using.
容量是向量当前使用的空间量。
Let's understand it with a very simple example:
让我们用一个非常简单的例子来理解它:
using namespace std;
int main(){
vector<int > vec;
vec.push_back(1);
vec.push_back(1);
vec.push_back(1);
cout<<"size of vector"<<vec.size()<<endl;
cout<<"capacity of vector"<<vec.capacity()<<endl;
return 0;
}
currently size is 3 and capacity is 4.
当前大小为 3,容量为 4。
Now if we push back one more element,
现在如果我们再推回一个元素,
using namespace std;
int main(){
vector<int> vec;
vec.push_back(1);
vec.push_back(1);
vec.push_back(1);
vec.push_back(1);
cout<<"size of vector"<<vec.size()<<endl;
cout<<"capacity of vector"<<vec.capacity()<<endl;
return 0;
}
now size is: 4 capacity is 4
现在大小是:4 容量是 4
now if we try to insert one more element in vector
then size will become 5 but capacity will become 8.
现在,如果我们尝试再插入一个元素,vector
那么大小将变为 5,但容量将变为 8。
it happens based on the datatype of vector
, as here in this case vector
in of type int
, as we know size of int
is 4 bytes so compiler will allocate 4 block of memory ..and when we try to add 5th element , vector::capacity()
is doubled what we have currently.
它是基于 的数据类型发生的vector
,就像在这种情况下vector
的类型一样int
,因为我们知道大小int
是 4 个字节,因此编译器将分配 4 个内存块..当我们尝试添加第 5 个元素时,vector::capacity()
是我们当前拥有的两倍.
same keep on..for example : if we try to insert 9th element then size of vector
will be 9 and capacity will b 16..
同样继续..例如:如果我们尝试插入第 9 个元素,则大小为vector
9,容量为 16 ..
回答by Oliver Charlesworth
size()
tells you how many elements you currently have. capacity()
tells you how large the size can get before the vector needs to reallocate memory for itself.
size()
告诉您当前有多少个元素。 capacity()
告诉您在向量需要为自己重新分配内存之前大小可以达到多大。
Capacity is always greater than or equal to size. You cannot index beyond element # size()-1
.
容量总是大于或等于大小。您不能索引超出元素 # size()-1
。
回答by Pete
The size is the number of elements in the vector. The capacity is the maximum number of elements the vector can currently hold.
大小是向量中元素的数量。容量是向量当前可以容纳的最大元素数。