C++ std::vector<T>::clear() 是否调用内容的析构函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22709344/
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 00:10:12  来源:igfitidea点击:

Does std::vector<T>::clear() call destructor of the content?

c++vector

提问by Dr. Debasish Jana

If I have a std::vector of something (say,does std::vector::clear() call destructor of the content? For example, if I have a data as vector, is the following code valid?

如果我有一个 std::vector 的东西(比如,std::vector::clear() 是否调用内容的析构函数?例如,如果我有一个数据作为向量,下面的代码是否有效?

char * pd;
for(vector<char *>::iterator it = data.begin(); it != data.end(); ++it) {
  pd = *it;
  delete pd;
}

Or, is it redundant as data.clear() would do that? I presume not, i.e. manual deallocation of the content is needed. I presume, data.clear() will not call implicit destructor for the contents? Is it correct? This is for pre C++11.

或者,它是否像 data.clear() 那样多余?我认为不是,即需要手动释放内容。我认为 data.clear() 不会为内容调用隐式析构函数?这是正确的吗?这是针对 C++11 之前的。

回答by Arne Mertz

To answer your title: std::vector<T>::clear()does call the destructor for each element. However, for primitive datatypes like char*the destructor is trivial (standardese for "does nothing") which is practically the same as if no destructor is called.

回答你的标题:std::vector<T>::clear()确实为每个元素调用析构函数。然而,对于像char*析构函数这样的原始数据类型来说是微不足道的(标准语为“什么都不做”),这实际上与没有调用析构函数一样。

Note: A destructor is not the same as the deleteoperator.

注意:析构函数与delete运算符不同。

So what your question should be: "Does std::vector<T*>::clear()call deleteon its elements?"and here the answer is: No, it does not call delete. You have to do it manually like shown in your example code, or you can (and probably should) use objects like std::stringor smart pointers instead of raw pointers. Those do the memory management for you, and their destructors (which get called, see above) call the deletefor you.

那么你的问题应该是:“是否std::vector<T*>::clear()调用delete了它的元素?” 答案是:不,它不会调用delete. 您必须像示例代码中显示的那样手动执行此操作,或者您可以(并且可能应该)使用像std::string或智能指针这样的对象而不是原始指针。那些为你做内存管理,它们的析构函数(被调用,见上文)delete为你调用。

回答by mathematician1975

If your items are dynamically allocated and your vector contains pointers to them, you will have to manually call deleteon each pointer in that container before calling clear(). Otherwise you have a memory leak.

如果您的项目是动态分配的,并且您的向量包含指向它们的指针,则您必须delete在调用之前手动调用该容器中的每个指针clear()。否则你有内存泄漏。

If they are objects that are not dynamically allocated then you do not need to do anything - the destructor (if there is one) of the objects in the vector will be called when the vector is cleared.

如果它们是未动态分配的对象,那么您不需要做任何事情——当向量被清除时,向量中对象的析构函数(如果有的话)将被调用。

回答by DrD

std::vector clear() does call the destructor of each element in the vector:

std::vector clear() 确实调用了向量中每个元素的析构函数:

http://www.cplusplus.com/reference/vector/vector/clear/

http://www.cplusplus.com/reference/vector/vector/clear/

But in your case it won't delete the contents of your dinamically allocated memory. I suggest using stringinstead of char*

但是在您的情况下,它不会删除动态分配的内存的内容。我建议使用string而不是char*