std :: vector复制后的容量

std :: vector复制后的容量

问题描述:


  • vector :: operator =更改向量容量?

  • Does vector::operator= change vector capacity? If so, how?
  • Does vector's copy constructor copy capacity?

我通过文档浏览了但找不到具体的答案。

I looked through documentation but could not find a specific answer. Is it implementation dependent?

您可以保证:


  1. 向量具有足够的容量来存储其元素。 (显然)

  2. 向量在当前容量满之前不会获得新容量。

因此,一个实现需要多少额外的或少量的东西取决于实现。我认为大多数会使容量匹配大小,当复制时,但不能降低容量。 (因为上面的数字2;重新分配,虽然有足够的空间是不允许的。)

So how much extra or little an implementation wants to put is up to the implementation. I think most will make capacity match size, when copying, but it cannot lower capacity. (Because of number 2 above; reallocating while there's enough room is not allowed.)

*大部分。见下面Charles的评论。