在C++中,向量函数push_back会增加空数组的大小吗?
快问。假设我声明了一个大小为 20 的向量。然后我想使用 Push_back 添加一些整数。
vector<int> myVector(20);
myVector.push_back(5);
myVector.push_back(14);
我的向量的容量现在是22,还是仍然是20? 5 和 14 是否分别添加到索引 [19] 和 [20] 中?或者它们在 [0] 和 [1] 处?
Quick question. Let's say I declare a vector of size 20. And then I want to add a few integers to it using push_back.
vector<int> myVector(20);
myVector.push_back(5);
myVector.push_back(14);
Is the capacity of my vector now 22, or is it still 20? Were 5 and 14 added to indices [19] and [20], respectively? Or are they at [0] and [1]?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在这些语句之后,其容量是由实现定义的。 (请注意,这与其大小不同。)
这将创建一个充满二十个 0 的向量。准确地说,它的大小是二十,容量至少是二十。是否正好是二十是由实现定义的;它可能有更多(实际上可能没有)。
此后,数组的第二十一个元素是 5,并且容量再次由实现定义。 (如果容量之前正好是 20,那么现在会以未指定的方式增加。)
同样,现在数组的第 22 个元素是 14,并且容量是实现定义的。
如果你想保留空间,但不想插入元素,你可以这样做:
After those statements its capacity is implementation-defined. (Please note that is different from its size.)
This creates a vector filled with twenty 0's. Its size is twenty, exactly, and its capacity is at least twenty. Whether or not it's exactly twenty is implementation-defined; it may have more (probably not, in practice).
After this, the twenty-first element of the array is 5, and the capacity is once again implementation-defined. (If the capacity had been exactly twenty before, it is now increased in an unspecified manner.)
Likewise, now the twenty-second element of the array is 14, and the capacity is implementation-defined.
If you want to reserve space, but not insert elements, you'd do it like this:
size
是向量容器中的元素数量。capacity
是分配的存储空间的大小push_back
有效地将向量大小增加一,如果向量大小等于向量,这会导致内部分配存储的重新分配呼叫前的容量。更多信息:http://www.cplusplus.com/reference/stl/vector/
size
is the number of elements in the vector container.capacity
is the size of the allocated storage spacepush_back
effectively increases the vector size by one, which causes a reallocation of the internal allocated storage if the vector size was equal to the vector capacity before the call.More info: http://www.cplusplus.com/reference/stl/vector/
push_back
增加了std::vector
的大小,并将新元素放置在vector
的后面(其他容器也有一个>push_front
方法也在前面做同样的事情)。但是,向量的大小和容量之间存在差异。 大小指的是现在
向量
中实际有多少项; 容量是指向量
在不重新分配内存的情况下可以容纳的项目总数。如果您知道要添加多个元素并且不希望逐渐增加向量
,则可以reserve()
内存。push_back
increases the size of thestd::vector
and places the new elements at the back of thevector
(other containers also have apush_front
method to do the same thing at the front as well).However, there is a difference between the size and the capacity of a
vector
. The size refers to how many items are actually in thevector
right now; the capacity refers to the total number of items that thevector
can hold without reallocating memory. It's possible toreserve()
memory if you know that you're going to add several elements and don't want to grow thevector
piecemeal.由于向量不为空,但大小为 20(包含 20 个元素),并且您将 2 个元素
推入
后面,因此它现在包含22 个元素。但是新元素不是放置在索引 19 和 20 处,而是放置在索引 20 和 21 处。如果您确实只想为向量保留足够的内存来容纳 20 个元素(实际上不包含任何元素),以防止昂贵的重新分配,那么您应该 在这种情况
下,向量仍然是空的,但它有足够的内存来添加至少 20 个元素(例如,使用
push_back
),而无需重新分配其内部存储。在这种情况下,向量仅包含您push
ed_back
的 2 个元素。As the vector is not empty but has a size of 20 (contains 20 elements) and you
push
2 elements to theback
, it now contains 22 elements. But the new elements are not placed at indices 19 and 20, but 20 and 21.If you really want to only reserve enough memory for the vector to hold 20 elements (without actually containing any elements), to prevent costly reallocations, then you should call
In this case the vector is still empty, but it has enough memory to add at least 20 elements (using
push_back
, for example) without needing to reallocate its internal storage. In this case the vector only contains the 2 elements youpush
ed_back
.push_back
会将向量的容量增加到至少向量的新大小,但可能(即可能)稍大一些。由于
push_back
需要在 O(1) 摊销时间内运行,因此每次重新分配都将是旧容量的某个倍数。在典型的实现中,该倍数为 2。但未指定确切的容量增加。如果您需要精确控制容量,请使用
reserve
。...
重新阅读你的问题,我不确定你是否理解向量的大小和容量之间的区别。大小是元素的数量。容量是向量在不执行重新分配的情况下可以容纳的元素数量。也就是说,您可以在重新分配之前
push_back
capacity()-size()元素。在您的示例中,5 和 14 将分别出现在 myVector[20] 和 myVector[21] 处。
push_back
will increase the capacity of the vector to at least the new size of the vector, but possibly (i.e. probably) somewhat larger.Because
push_back
is required to run in O(1) amortized time, each reallocation will be to some multiple of the old capacity. In a typical implementation that multiple is 2.But the exact capacity increase is not specified. If you require precise control over the capacity, use
reserve
....
Re-reading your question, I am not sure you understand the difference between a vector's size and its capacity. The size is the number of elements. The capacity is the number of elements the vector can hold without performing a reallocation. That is, you can
push_back
capacity()-size() elements before a reallocation happens.In your example, 5 and 14 will appear at myVector[20] and myVector[21], respectively.
嗯,
vector
有成员函数push_back
。其他序列如deque
具有push_front
。你可能还记得:
也就是说,前面不能加或中间,因为向量专门用于通过索引快速访问元素。如果要前后添加,可以使用类似于
vector
的deque
。如果你想添加到前面、后面和任何地方,你可以使用list
。请注意,list
不提供像deque
和vector
那样的索引。然而,假设向量的容量大于其实际大小。当您向其中添加元素时,不需要分配额外的内存。仅当容量等于大小时才有效。在许多编译器上,新容量将是旧容量的两倍。分配后,它将所有元素复制到新位置。然而,这种行为在内存方面可能会很昂贵。
Well,
vector
has the member functionpush_back
. Other sequences likedeque
havepush_front
.You may recall that:
That is, you can not add at front or the middle, since the
vector
is specialized for quick access to elements by index. If you want to add at front and back, you can usedeque
which is similar tovector
. If you want to add to front, back and anywhere you can uselist
. Note thatlist
doesn't provide indexing likedeque
andvector
.However, a vector is assumed to have more capacity than its actual size. When you add elements to it, it doesn't necessary allocate additional memory. It does only if the capacity equals the size. On many compilers, the new capacity will be double of the old one. After allocating, it copies all elements in the new location. Such behavior may be expensive in terms of memory, however.