复制后的 std::vector 容量 [英] std::vector capacity after copying

查看:34
本文介绍了复制后的 std::vector 容量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • vector::operator= 是否改变了矢量容量?如果是这样,怎么办?
  • vector 的复制构造函数是否复制容量?

我查看了文档,但找不到具体的答案.它是否依赖于实现?

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

推荐答案

所有你保证的是:

  1. 向量有足够的容量来存储其元素.(显然.)
  2. 在当前容量已满之前,vector 不会获得新容量.*

因此,一个实现想要投入多少额外或多少取决于实现.我认为大多数会在复制时使容量匹配大小,但不能降低容量.(因为上面的数字 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.)

* 主要是.请参阅下面的查尔斯评论.

这篇关于复制后的 std::vector 容量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆