std :: vector的元素的销毁顺序 [英] Order of destruction of elements of an std::vector

查看:206
本文介绍了std :: vector的元素的销毁顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

STL容器元素销毁订单

std :: vector 的元素将从最后到第一个被销毁?

Is there a guarantee the the elements of an std::vector would be destroyed from last to first?

推荐答案

2003:5.3.5 / 6说关于 delete []

2003:5.3.5/6 says about delete[]:


delete-expression将为对象或要删除的数组的元素调用析构函数(如果有)。在数组的情况下,元素将按照地址减小的顺序被破坏(即,以它们的构造函数的完成的相反顺序;见12.6.2)。

The delete-expression will invoke the destructor (if any) for the object or the elements of the array being deleted. In the case of an array, the elements will be destroyed in order of decreasing address (that is, in reverse order of the completion of their constructor; see 12.6.2).

所以如果你的 std :: vector 对象的分配器使用 delete [] 然后,是的,它必须以相反的顺序销毁元素。

So if your std::vector object's allocator uses delete[] then, yes, it is bound to destroy elements in reverse order.

但是,不能保证您的 std :: vector 将以这种方式工作(事实上,它很可能不会),我找不到任何特定于容器的引用。

However, there is no guarantee that your std::vector will work this way (and, in fact, it most likely does not), and I can't find any citations specific to the container.

真的,我认为这一切都在你的分配器和2003:20.1.5(列出了对分配器的要求)似乎没有说什么。

Really, I think it's all down to your allocator and 2003:20.1.5 (which lists the requirements placed upon allocators) doesn't appear to say anything about it.

这篇关于std :: vector的元素的销毁顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆