简体   繁体   English

C++ STD 容器:std::vector - 有没有办法清除向量并保留存储分配?

[英]C++ STD Containers: std::vector - Is there a way to clear the vector and retain storage allocation?

Is there a way to clear the contents of std::vector while retaining the memory allocation?有没有办法在保留内存分配的同时清除std::vector的内容?

The reason for this is that I have a section of code which loops, and I wish to optimize this.这样做的原因是我有一段代码循环,我希望优化它。 I currently have data stored in std::vector s.我目前将数据存储在std::vector Each time the loop begins the contents of the vector should be cleared.每次循环开始时,应清除向量的内容。 I then use push_back to enter data into the vector, however since the length of the vector does not often change between iterations of the loop, I would ideally like the vector to retain its allocated storage in memory.然后我使用push_back将数据输入到向量中,但是由于向量的长度在循环的迭代之间不经常改变,我理想情况下希望向量将其分配的存储保留在内存中。

If this is not possible with vector is it possible with another fast access STD type?如果vector无法做到这一点,是否可以使用另一种快速访问 STD 类型?

Is there a way to clear the contents of std::vector while retaining the memory allocation?有没有办法在保留内存分配的同时清除 std::vector 的内容?

Yes you call std::vector::clear() method, as it is stated in documentation :是的,您调用std::vector::clear()方法,如文档中所述

Leaves the capacity() of the vector unchanged保持向量的容量()不变

First have look at here:首先看看这里:

Does clearing a vector affect its capacity? 清除向量会影响其容量吗?

So the standard does not seem to mandate that the capacity is unchanged after clear.所以标准似乎并没有规定容量在清除后不变。 But it usually does so in practice.但在实践中通常会这样做。

You can do two things however:但是,您可以做两件事:

  1. use reseve on the vector at the beginning of the loop, so that you only have one allocation per loop.在循环开始时在向量上使用 reseve,这样每个循环只有一个分配。 This is quite simple.这很简单。
  2. You can use your vector in an indexed way.您可以以索引方式使用您的向量。 This seems to be more optimal, but harder to do.这似乎更理想,但更难做到。

UPDATE: problem with the answer provided here:更新:此处提供的答案有问题:

What does the standard say about how calling clear on a vector changes the capacity? 标准对矢量调用 clear 如何改变容量有什么说法?

The justification in the answer is based on this: No reallocation shall take place during insertions that happen after a call to reserve() until an insertion would make the size of the vector greater than the value of capacity().答案中的理由基于此:在调用 Reserve() 之后发生的插入期间不应进行重新分配,直到插入将使向量的大小大于 capacity() 的值。

This is in the C++ standard.这是在 C++ 标准中。

You can try this:你可以试试这个:

v.reserve(v.capacity());
v.clear();

However the standard does not specify if the capacity is changed by clear, nor does it specify if the clause for reserve refers to the capacity at the time of the reseve call, or at the time of insert.但是该标准没有规定容量是否通过 clear 改变,也没有规定 Reserve 子句是指调用 reseve 时的容量,还是插入时的容量。

UPDATE: for a relevant discussion about this issue on the C++ standards comittee discussion board, please see this: https://cplusplus.github.io/LWG/issue1102更新:有关 C++ 标准委员会讨论板上关于此问题的相关讨论,请参阅: https ://cplusplus.github.io/LWG/issue1102

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM