向量的reserve()方法的行为
我想知道 std::vector::reserve()
在以下情况下的行为:
- 假设
reserve(N)
立即被多次调用。 之前的reserve(N1)
会被添加还是覆盖? - 如果较早的
reserve(N1)
被最新的调用覆盖, 那么如果最新的reserve(Nn)
需要更少数量的槽位,会发生什么? - 声明
vector
后,如果我们只有push_back()
X 元素, 然后我们调用reserve(N)。已经push_back()
X 元素计数为N
? - 假设,如果
向量
有一些X推送的元素,现在如果我们push_back()
再多 1 个元素 (X+1),则该对象必须 搬迁;但我们还没有执行push_back()
。什么 如果我们现在调用reserve()
会发生什么吗?对象会被重新定位吗 立即地 ?如果没有,那么空间是如何保留的?
I wanted to know the behavior of std::vector::reserve()
in following situations:
- Suppose
reserve(N)
is called multiple times one after another immediately.
Will the earlierreserve(N1)
get added up or overwritten ? - If the earlier
reserve(N1)
gets overwritten with the latest call,
then what happens if the latestreserve(Nn)
demands less number of slots ? - After declaring
vector
if we have simplypush_back()
X elements,
and then we callreserve(N)
. Will the alreadypush_back()
X
elements counted inN
? - Suppose, if the
vector
has some X pushed elements and now if wepush_back()
1 more element (X+1), then that object would have to
get relocated; but we haven't yet performedpush_back()
. What
happens if we callreserve()
now ? Will the object get relocated
immediately ? If not, then how is the space reserved ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
reserve()
只是确保向量分配的内存至少足够大以包含作为其参数传递的项目数。因此...reserve()
时,向量会分配尽可能多的内存来存储传递给reserve()
的项目数。引用实际标准:
reserve()
just makes sure that the vector's allocated memory is at least large enough to contain the number of items passed as its argument. Thus...reserve()
is called as is necessary to store the number of items passed toreserve()
.To quote from the actual standard:
与
std::string
不同,std::vector
无法调用reserve()
来缩小capacity()< /code>.使用小于当前
capacity()
的参数调用reserve()
是无操作。因此,增加当前容量的最后一个reserve()
调用将保持有效。使用小于当前
capacity()
的参数调用reserve()
是无操作。reserve()
只是分配(保留)足够数量的元素,所以是的。请注意,调用reserve()
后,仅更改向量的capacity()
,size()
保持不受影响。如果您需要创建尽可能多的元素,而不仅仅是保留您应该使用resize()
的内存。是的,搬迁将会发生,但这要视情况而定。如前所述,reserve() 分配足够的内存来存储与传递给它的参数一样多的元素。因此,如果这个元素数量大于当前向量
capacity()
所能容纳的数量,就会发生重定位。标准参考:
C++03 23.2.4.2 向量容量 [lib.vector.capacity]
Unlike
std::string
it is not possible to callreserve()
forstd::vector
to shrink thecapacity()
.Callingreserve()
with an argument that is less than the currentcapacity()
is a no-op. Hence the lastreserve()
call which increases the current capacity will hold good.Calling
reserve()
with an argument that is less than the currentcapacity()
is a no-op.reserve()
just allocates(reserves) enough number of elements so Yes. Note that after callingreserve()
only thecapacity()
of the vector is changed thesize()
remains unaffected.If you would need to create as many elements and not just reserve memory you should be usingresize()
.Yes, the relocation will happen but it depends. As said before,
reserve()
allocates enough memory to store as many elements as the argument passed to it. So if this number of elements is greater than what can be accommodated in current vectorcapacity()
, relocation will happen.Standard References:
C++03 23.2.4.2 vector capacity [lib.vector.capacity]