向量的reserve()方法的行为

发布于 2025-01-05 16:28:29 字数 600 浏览 0 评论 0原文

我想知道 std::vector::reserve() 在以下情况下的行为:

  1. 假设 reserve(N) 立即被多次调用。 之前的 reserve(N1) 会被添加还是覆盖?
  2. 如果较早的 reserve(N1) 被最新的调用覆盖, 那么如果最新的 reserve(Nn) 需要更少数量的槽位,会发生什么?
  3. 声明 vector 后,如果我们只有 push_back() X 元素, 然后我们调用reserve(N)。已经 push_back() X 元素计数为 N
  4. 假设,如果向量有一些X推送的元素,现在如果我们 push_back() 再多 1 个元素 (X+1),则该对象必须 搬迁;但我们还没有执行 push_back()。什么 如果我们现在调用 reserve() 会发生什么吗?对象会被重新定位吗 立即地 ?如果没有,那么空间是如何保留的?

I wanted to know the behavior of std::vector::reserve() in following situations:

  1. Suppose reserve(N) is called multiple times one after another immediately.
    Will the earlier reserve(N1) get added up or overwritten ?
  2. If the earlier reserve(N1) gets overwritten with the latest call,
    then what happens if the latest reserve(Nn) demands less number of slots ?
  3. After declaring vector if we have simply push_back() X elements,
    and then we call reserve(N). Will the already push_back() X
    elements counted in N ?
  4. Suppose, if the vector has some X pushed elements and now if we
    push_back() 1 more element (X+1), then that object would have to
    get relocated; but we haven't yet performed push_back(). What
    happens if we call reserve() now ? Will the object get relocated
    immediately ? If not, then how is the space reserved ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

°如果伤别离去 2025-01-12 16:28:29

reserve() 只是确保向量分配的内存至少足够大以包含作为其参数传递的项目数。因此...

  1. 无论所有通过的最大值是什么,都将是有效保留的最小结果容量。
  2. 参见#1。
  3. 是的。
  4. 当调用 reserve() 时,向量会分配尽可能多的内存来存储传递给 reserve() 的项目数。

引用实际标准:

void reserve(size_type n)

如果n小于或等于capacity(),则此调用无效。
否则,它是分配额外内存的请求。如果
请求成功,则capacity()大于等于n
否则,capacity() 不变。无论哪种情况,size() 都是
不变。

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

  1. Whichever the max value of all those passed will be the minimum resulting capacity effectively reserved.
  2. See #1.
  3. Yes.
  4. The vector allocates as much memory when reserve() is called as is necessary to store the number of items passed to reserve().

To quote from the actual standard:

void reserve(size_type n)

If n is less than or equal to capacity(), this call has no effect.
Otherwise, it is a request for allocation of additional memory. If the
request is successful, then capacity() is greater than or equal to n;
otherwise, capacity() is unchanged. In either case, size() is
unchanged.

温柔戏命师 2025-01-12 16:28:29

假设reserve(N)立即被相继调用多次。之前的 reserve(N1) 会被添加还是覆盖?

std::string 不同,std::vector 无法调用 reserve() 来缩小 capacity()< /code>.使用小于当前 capacity() 的参数调用 reserve() 是无操作。因此,增加当前容量的最后一个 reserve() 调用将保持有效。

如果较早的 reserve(N1) 被最新的调用覆盖,那么如果最新的 reserve(Nn) 需要更少数量的槽,会发生什么?

使用小于当前 capacity() 的参数调用 reserve() 是无操作。

声明向量后,如果我们只有push_back() X个元素,然后我们调用reserve(N)。已经push_back()的X个元素会计入N吗?

reserve() 只是分配(保留)足够数量的元素,所以是的。请注意,调用 reserve() 后,仅更改向量的 capacity()size() 保持不受影响。如果您需要创建尽可能多的元素,而不仅仅是保留您应该使用resize()的内存。

假设,如果向量有一些 X 推送元素,现在如果我们 push_back() 还有 1 个元素 (X+1),那么该对象将不得不重新定位;但我们还没有执行 push_back()。如果我们现在调用 reserve() 会发生什么?该对象会立即重新定位吗?如果没有,那么空间是如何保留的?

是的,搬迁将会发生,但这要视情况而定。如前所述,reserve() 分配足够的内存来存储与传递给它的参数一样多的元素。因此,如果这个元素数量大于当前向量capacity()所能容纳的数量,就会发生重定位。

标准参考:
C++03 23.2.4.2 向量容量 [lib.vector.capacity]

void Reserve(size_type n);

效果:通知向量计划的大小更改的指令,以便它可以相应地管理存储分配。在reserve()之后,如果发生重新分配,capacity()大于或等于reserve的参数;否则等于之前的capacity() 值。当且仅当当前容量小于 reserve() 的参数时,此时才会发生重新分配。

复杂性:它不会改变序列的大小,并且最多花费序列大小的线性时间。

抛出: length_error 如果 n > max_size().248)

注意:重新分配会使引用序列中元素的所有引用、指针和迭代器无效。确保在调用 reserve() 后发生的插入期间不会发生重新分配,直到插入使向量的大小大于最近调用中指定的大小为止reserve()

Suppose reserve(N) is called multiple times one after another immediately. Will the earlier reserve(N1) get added up or overwritten ?

Unlike std::string it is not possible to call reserve() for std::vector to shrink the capacity().Calling reserve() with an argument that is less than the current capacity() is a no-op. Hence the last reserve() call which increases the current capacity will hold good.

If the earlier reserve(N1) gets overwritten with the latest call, then what happens if the latest reserve(Nn) demands less number of slots ?

Calling reserve() with an argument that is less than the current capacity() is a no-op.

After declaring vector if we have simply push_back() X elements, and then we call reserve(N). Will the already push_back() X elements counted in N ?

reserve() just allocates(reserves) enough number of elements so Yes. Note that after calling reserve() only the capacity() of the vector is changed the size() remains unaffected.If you would need to create as many elements and not just reserve memory you should be using resize().

Suppose, if the vector has some X pushed elements and now if we push_back() 1 more element (X+1), then that object would have to get relocated; but we haven't yet performed push_back(). What happens if we call reserve() now ? Will the object get relocated immediately ? If not, then how is the space reserved ?

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 vector capacity(), relocation will happen.

Standard References:
C++03 23.2.4.2 vector capacity [lib.vector.capacity]

void reserve(size_type n);

Effects: A directive that informs a vector of a planned change in size, so that it can manage the storage allocation accordingly. After reserve(), capacity() is greater or equal to the argument of reserve if reallocation happens; and equal to the previous value of capacity() otherwise. Reallocation happens at this point if and only if the current capacity is less than the argument of reserve().

Complexity: It does not change the size of the sequence and takes at most linear time in the size of the sequence.

Throws: length_error if n > max_size().248)

Notes: Reallocation invalidates all the references, pointers, and iterators referring to the elements in the sequence. It is guaranteed that no reallocation takes place during insertions that happen after a call to reserve() until the time when an insertion would make the size of the vector greater than the size specified in the most recent call to reserve().

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