在C++中,向量函数push_back会增加空数组的大小吗?

发布于 2024-12-10 11:09:09 字数 238 浏览 0 评论 0原文

快问。假设我声明了一个大小为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

画骨成沙 2024-12-17 11:09:09

在这些语句之后,其容量是由实现定义的。 (请注意,这与其大小不同。)


vector<int> myVector(20);

这将创建一个充满二十个 0 的向量。准确地说,它的大小是二十,容量至少是二十。是否正好是二十是由实现定义的;它可能有更多(实际上可能没有)。

myVector.push_back(5);

此后,数组的第二十一个元素是 5,并且容量再次由实现定义。 (如果容量之前正好是 20,那么现在会以未指定的方式增加。)

myVector.push_back(14);

同样,现在数组的第 22 个元素是 14,并且容量是实现定义的。


如果你想保留空间,但不想插入元素,你可以这样做:

vector<int> myVector;
myVector.reserve(20); // capacity is at least twenty, guaranteed not
                      // to reallocate until after twenty elements are pushed

myVector.push_back(5); // at index zero, capacity at least twenty.
myVector.push_back(14); // at index one, capacity at least twenty.

After those statements its capacity is implementation-defined. (Please note that is different from its size.)


vector<int> myVector(20);

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).

myVector.push_back(5);

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.)

myVector.push_back(14);

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:

vector<int> myVector;
myVector.reserve(20); // capacity is at least twenty, guaranteed not
                      // to reallocate until after twenty elements are pushed

myVector.push_back(5); // at index zero, capacity at least twenty.
myVector.push_back(14); // at index one, capacity at least twenty.
蓝戈者 2024-12-17 11:09:09
  • 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 space
  • push_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/

戈亓 2024-12-17 11:09:09

push_back 增加了 std::vector 的大小,并将新元素放置在 vector 的后面(其他容器也有一个 >push_front 方法也在前面做同样的事情)。

但是,向量的大小和容量之间存在差异。 大小指的是现在向量中实际有多少项; 容量是指向量在不重新分配内存的情况下可以容纳的项目总数。如果您知道要添加多个元素并且不希望逐渐增加向量,则可以reserve() 内存。

push_back increases the size of the std::vector and places the new elements at the back of the vector (other containers also have a push_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 the vector right now; the capacity refers to the total number of items that the vector can hold without reallocating memory. It's possible to reserve() memory if you know that you're going to add several elements and don't want to grow the vector piecemeal.

十秒萌定你 2024-12-17 11:09:09

由于向量不为空,但大小为 20(包含 20 个元素),并且您将 2 个元素推入后面,因此它现在包含22 个元素。但是新元素不是放置在索引 19 和 20 处,而是放置在索引 20 和 21 处。

如果您确实只想为向量保留足够的内存来容纳 20 个元素(实际上不包含任何元素),以防止昂贵的重新分配,那么您应该 在这种情况

std::vector<int> myVector;
myVector.reserve(20);

下,向量仍然是空的,但它有足够的内存来添加至少 20 个元素(例如,使用 push_back),而无需重新分配其内部存储。在这种情况下,向量仅包含您 pushed_back 的 2 个元素。

As the vector is not empty but has a size of 20 (contains 20 elements) and you push 2 elements to the back, 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

std::vector<int> myVector;
myVector.reserve(20);

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 you pushed_back.

且行且努力 2024-12-17 11:09:09

push_back 会将向量的容量增加到至少向量的新大小,但可能(即可能)稍大一些。

由于 push_back 需要在 O(1) 摊销时间内运行,因此每次重新分配都将是旧容量的某个倍数。在典型的实现中,该倍数为 2。

但未指定确切的容量增加。如果您需要精确控制容量,请使用reserve

...

重新阅读你的问题,我不确定你是否理解向量的大小和容量之间的区别。大小是元素的数量。容量是向量在不执行重新分配的情况下可以容纳的元素数量。也就是说,您可以在重新分配之前push_backcapacity()-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.

滥情稳全场 2024-12-17 11:09:09

嗯,vector 有成员函数push_back。其他序列如 deque 具有 push_front

0、1、2、......、最终

添加后:

0、1、2、.....、最终、加法、...

你可能还记得:

capacity() returns the number of elements in the vector sufficient,
without allocating additional memory.
This number can be greater or equal to size.

也就是说,前面不能加或中间,因为向量专门用于通过索引快速访问元素。如果要前后添加,可以使用类似于vectordeque。如果你想添加到前面、后面和任何地方,你可以使用list。请注意,list 不提供像 dequevector 那样的索引。

然而,假设向量的容量大于其实际大小。当您向其中添加元素时,不需要分配额外的内存。仅当容量等于大小时才有效。在许多编译器上,新容量将是旧容量的两倍。分配后,它将所有元素复制到新位置。然而,这种行为在内存方面可能会很昂贵。

Well, vector has the member function push_back. Other sequences like deque have push_front.

0, 1, 2, ......, final

after addition:

0, 1, 2, ....., final, addition, ...

You may recall that:

capacity() returns the number of elements in the vector sufficient,
without allocating additional memory.
This number can be greater or equal to size.

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 use deque which is similar to vector. If you want to add to front, back and anywhere you can use list. Note that list doesn't provide indexing like deque and vector.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文