为什么不在 GotW 54 中调整大小并清除作品?

发布于 2024-08-11 03:29:34 字数 324 浏览 7 评论 0原文

参考 HerbSutter 的文章 Gotw 54,他解释了

  1. 正确的方法“缩小以适应” 矢量或双端队列和

  2. 完全清除向量或的正确方法 deque

我们可以只使用container.resize() 和用于上述任务的 container.clear() 或者我错过了什么?

Referring to article Gotw 54 by HerbSutter, he explains about

  1. The Right Way To "Shrink-To-Fit" a
    vector or deque and

  2. The Right Way to Completely Clear a vector or
    deque

Can we just use container.resize()
and container.clear() for the above task
or am I missing something?

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

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

发布评论

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

评论(3

十六岁半 2024-08-18 03:29:34

向量包含两种不同的东西:大小容量。如果您只是调整向量的大小,则不能保证容量(保留多少内存)一定会改变。 resize 是一个与您使用了多少容量相关的操作,而不是矢量容量有多少。

例如。

size     == how much you are using
capacity == how much memory is reserved
vector<int> v(10);

v.resize(5); // size == 5 but capacity (may or may) not be changed
v.clear()    // size == 0 but capacity (may or may) not be changed

最后,不应该在每次操作时都改变容量,因为这会带来大量的内存分配/释放开销。他是说,如果您需要“释放”向量保留的内存,就这样做。

There are two different things that a vector holds: size Vs capacity. If you just resize the vector, there is no guarantee that the capacity(how much memory is reserved) must change. resize is an operation concerned with how much are you using, not how much the vector capacity is.

So for example.

size     == how much you are using
capacity == how much memory is reserved
vector<int> v(10);

v.resize(5); // size == 5 but capacity (may or may) not be changed
v.clear()    // size == 0 but capacity (may or may) not be changed

In the end, capacity should not changed on every operation, because that would bring a lot of memory allocation/deallocation overhead. He is saying that if you need to "deallocate" the memory reserved by vector, do that.

油焖大侠 2024-08-18 03:29:34

resize() 和clear() 都不起作用。向量的 .capacity() 保证至少与向量的当前 size() 一样大,并且保证至少与向量的当前 size() 一样大reserve()d 容量。此外,这个 .capacity() 不会缩小,因此它也至少与之前的 size()reserve() 一样大>化。

现在,向量的 .capacity() 只是它保留的内存。通常并非所有内存都包含对象。调整大小会删除对象,但不会回收内存。向量只能在分配更大的缓冲区时回收其内存缓冲区。

交换技巧的工作原理是将所有对象复制到更小、更合适的内存缓冲区。之后,原来的内存缓冲区就可以被回收。这似乎违反了前面的说法,即向量的内存缓冲区只能增长。但是,通过交换技巧,您暂时拥有 2 个向量。

Neither resize() nor clear() work. The .capacity() of a vector is guaranteed to be at least as big as the current size() of the vector, and guaranteed to be at least as big as the reserve()d capacity. Also, this .capacity() doesn't shrink, so it is also at least as big as any previous size() or reserve()ation.

Now, the .capacity() of a vector is merely the memory it has reserved. Often not all of that memory cotains objects. Resizing removes objects, but doesn't recycle the memory. A vector can only recycle its memory buffer when allocating a larger buffer.

The swap trick works by copying all ojects to a smaller, more appropriate memory buffer. Afterwards, the original memory buffer can be recycled. This appears to violate the previous statement that the memory buffer of a vector can only grow. However, with the swap trick, you temporarily have 2 vectors.

葮薆情 2024-08-18 03:29:34

向量有大小和容量。它可能保存 X 个元素,但还为 Y 元素存储了未初始化的内存。在典型的实现中,擦除、调整大小(当调整为较小尺寸时)和清除不会影响容量:如果您想稍后向其中添加新项目,向量会保留内存。

The vector has size and capacity. It may hold X elements but have uninitialized memory in store for Y elements more. In a typical implementation erase, resize (when resizing to smaller size) and clear don't affect the capacity: the vector keeps the memory around for itself, should you want to add new items to it at a later time.

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