如何缩小 std::vector 的大小?

发布于 2024-07-07 20:28:11 字数 64 浏览 6 评论 0原文

当我不再需要以前保留的空间时,有没有办法调整 std::vector 的大小以降低容量?

Is there a way to resize a std::vector to lower capacity when I no longer need previously reserved space?

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

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

发布评论

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

评论(5

快乐很简单 2024-07-14 20:28:11

《有效的 STL》,作者:Scott Meyers,第 17 条:使用交换技巧来削减多余的容量。

vector<Person>(persons).swap(persons);

之后,persons 被“缩小以适应”。

这依赖于这样一个事实:vector 的复制构造函数仅分配复制元素所需的内存。

Effective STL, by Scott Meyers, Item 17: Use the swap trick to trim excess capacity.

vector<Person>(persons).swap(persons);

After that, persons is "shrunk to fit".

This relies on the fact that vector's copy constructor allocates only as much as memory as needed for the elements being copied.

稚然 2024-07-14 20:28:11

如果您使用的是 C++11,则可以使用vec.shrink_to_fit()。 至少在 VS2010 中,这可以为您完成交换技巧。

If you're using C++11, you can use vec.shrink_to_fit(). In VS2010 at least, that does the swap trick for you.

飘落散花 2024-07-14 20:28:11

从现有向量创建一个新的临时向量,然后对现有向量调用交换方法,将临时向量传入。让临时向量(现在带有旧的、超大的缓冲区)超出范围。

嘿,很快,您的矢量的大小与其内容完全正确。

如果这听起来像是大量的复制和分配 - 请记住,这就是向量每次必须重新分配超过其当前保留限制时所做的事情。

[编辑]
是的,我只是用更多的话和塞巴斯蒂安说了同样的话。 stackoverflow 竞争条件的另一种情况;-)

Create a new, temporary, vector from the existing one then call the swap method on the existing one, passing the temporary one in. Let the temporary (now with the old, oversized, buffer) go out of scope.

Hey presto, your vector has exactly the right size for its contents.

If this sounds like a lot of copying and allocation - bear in mind that this is what vector does every time it has to realloc past its current reserved limit anyway.

[Edit]
Yes, I just said the same as Sebastien in more words. Another case of stackoverflow race-condition ;-)

找回味觉 2024-07-14 20:28:11

交换技巧是减少对象容量的有效方法,
它通过复制构造将我的向量的内容与新创建的向量交换:

vector<Person>(persons).swap(persons);

请注意,不能保证 person.capacity(); 交换技巧之后等于
大小:向量(人)的容量是图书馆实施的容量
保留大小为 people.size() 的向量。

C++11 引入了 shrink_to_fit()

Shrink_to_fit() 以及交换技巧并不能保证容量大小有效
减小到向量的大小。

无论如何,shrink_to_fit() 可以使您的迭代器无效(如果发生重新分配)或不能:
这取决于库的实际实现。

请记住,交换技巧需要 Person 和 Person.size() 的复制结构
person.size() 破坏。 Shrink_to_fit() 可以避免所有这些复制,并且可以
让你的迭代器保持有效。 可以。 但时不时地会发生shrink_to_fit() 的实现
交换技巧的条款...

The swap trick is an effective way to reduce the capacity of an object,
it swaps the content of my vector with a newly created one by copy construction:

vector<Person>(persons).swap(persons);

Notice that there is no guarantee that persons.capacity(); after the swap trick is equal to
the size: the capacity of vector(persons) is the capacity the library implementation
reserves to vectors of size persons.size().

C++11 introduced shrink_to_fit().

shrink_to_fit() as well as the swap trick does not guarantee the capacity size is effectively
reduced to the size of the vector.

Anyway shrink_to_fit() can invalidate your iterators (if a reallocation happens) or cannot:
it depends on the actual implementation of the library.

Bear in mind that the swap trick requires persons.size() copy constructions of Person and
person.size() destructions. The shrink_to_fit() could avoid all this copying and could
leave your iterators valid. Could. But from time to time it happens that shrink_to_fit() is implemented in
terms of the swap trick...

も让我眼熟你 2024-07-14 20:28:11

您正在寻找 QVector::squeeze 的等效项,而我恐怕它在 STL 中并没有明确存在。
如果 Sébastien 的答案适合您的 STL 实现,请查找该答案。

You're looking for an equivalent of QVector::squeeze and I'm afraid it doesn't exist explicitely in the STL.
Go for Sébastien's answer if it is correct for your STL implementation.

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