如何从内存中清除 C++ 中的向量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35514909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:31:38  来源:igfitidea点击:

How to clear vector in C++ from memory

c++

提问by JokerMartini

I'm new to C++ so this may be a newbie question however I'm not entirely clear how to clear it from memory. I've searched online and looked are several answers but was unclear on how to properly execute what im trying to do.

我是 C++ 新手,所以这可能是一个新手问题,但是我不完全清楚如何从内存中清除它。我在网上搜索并查看了几个答案,但不清楚如何正确执行我正在尝试做的事情。

How can i clear my vector from memory correctly in c++? Thanks and i apologize if this is a dup post.

如何在 C++ 中正确地从内存中清除我的向量?谢谢,如果这是一个重复的帖子,我深表歉意。

vector<ElementData> Elements;

As mentioned by a comment below, i would like to return the memory used as well.

正如下面的评论所提到的,我也想返回使用的内存。

except that it will not return any memory to the system as the capacity remains unchanged.

除了因为容量保持不变,它不会向系统返回任何内存。

回答by NathanOliver

If you want to reset you vector back to a empty state then we can use the swaptrick to swap the contents of the vector into a temporary that will get destroyed and free the memory

如果您想将向量重置为空状态,那么我们可以使用该swap技巧将向量的内容交换为临时对象,该临时对象将被销毁并释放内存

vector<ElementData> Elements
// fill the vector up
vector<ElementData>().swap(Elements);

This will create a temporary empty vector, swap it with the one you have so now it is empty and then destroy everything in the temporary vector.

这将创建一个临时空向量,将其与您拥有的向量交换,因此现在它是空的,然后销毁临时向量中的所有内容。

You can see it working in this little example

你可以看到它在这个小例子中工作

int main() {
    std::vector<int> foo(500);
    std::cout << foo.capacity() << std::endl;
    std::vector<int>().swap(foo);
    std::cout << foo.capacity() << std::endl;
}       

Live Example

Live Example

回答by Johan Lundberg

std::vector variables are normally destructed automatically when you go out of scope, or when the class object they are members of are destroyed. The destructor of std::vector also destroys all elements in the vector.

std::vector 变量通常在超出范围时自动销毁,或者当它们所属的类对象被销毁时。std::vector 的析构函数也会破坏向量中的所有元素。

To completely clear a vector explicitly, first clear the vector (destroy the elements)

要明确地完全清除向量,首先清除向量(销毁元素)

v.clear()

then return the memory previously reserved for the vector elements

然后返回之前为向量元素保留的内存

v.shrink_to_fit();

http://en.cppreference.com/w/cpp/container/vector/shrink_to_fit

http://en.cppreference.com/w/cpp/container/vector/shrink_to_fit

Only call shrink_to_fitif it's likely that the vector previously contained far more elements than you are going to fill it with.

shrink_to_fit当向量之前包含的元素可能比您要填充的元素多得多时才调用。

回答by Chara

When your program ends, vector's dtor will get called and delete your vector for you, since you've allocated it on the stack. Furthermore, the dtor for ElementDatawill get called for each one in the vector.

当你的程序结束时,vector的 dtor 将被调用并为你删除你的向量,因为你已经在堆栈上分配了它。此外,ElementData将为向量中的每一个调用dtor for 。

回答by Ferruccio

In most cases, the best way is to limit the scope of the vector to where it's actually needed. So, instead of something like:

在大多数情况下,最好的方法是将向量的范围限制在实际需要的地方。所以,而不是像这样:

vector<Elements> Elements;
... do some work with Elements ...
Elements.clear(); // or whatever "trick" you want to use
... do more work where Elements is not needed ...

You would do this:

你会这样做:

{ // begin a new scope
    vector<Elements> Elements;
    ... do some work with Elements ...
} // Elements is now out of scope, its memory has been freed
... do more work where Elements is not needed ...

The other benefit of this is that after Elementshas gone out of scope, you can't accidentally reuse it. If the function is small, you probably don't need to create a new explicit scope. Returning from the function will have the same effect.

这样做的另一个好处是,在Elements超出范围之后,您不会意外地重用它。如果函数很小,您可能不需要创建新的显式作用域。从函数返回将具有相同的效果。

回答by Oskar Hybl

You don't have to do anything special. Since both the vector and the elements inside are statically allocated (i.e. not created by new), when the vector gets out of scope, it's deallocated and the destructor gets called on all ElementData inside. If you would have vector<ElementData*>, where the vector only contains pointers to ElementData, then you would also have to deleteall the ElementData yourself.

你不必做任何特别的事情。由于向量和里面的元素都是静态分配的(即不是由 new 创建的),当向量超出范围时,它会被释放并且析构函数被调用到里面的所有 ElementData 上。如果您有vector<ElementData*>,其中向量仅包含指向 ElementData 的指针,那么您还必须delete自己拥有所有 ElementData。

回答by Alex

Elements.clear()will do what you want.

Elements.clear()会做你想做的。

Hereare the rest of the functions available to vectors.

以下是向量可用的其余函数。