Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ vector::clear() - order of destruction? [duplicate]

Tags:

c++

c++11

stl

I've searched around and not found anything - does C++ give any guarantee on the order that items in a std::vector will be deleted when calling vector::clear()?

I have a vector with some items which depend on other items in the vector, so need to ensure it is cleared LIFO. FIFO would be fine - I can reverse the vector before calling clear().

like image 738
Brad Avatar asked Sep 11 '25 09:09

Brad


1 Answers

According to the sequence container requirements(std::vector is one of those) the standard only says this about clear():

Destroys all elements in a. Invalidates all references, pointers, and iterators referring to the elements of a and may invalidate the past-the-end iterator.

Ensures: a.empty() returns true.

Complexity: Linear.

So no, you get no guarantees on the order of destruction.

like image 113
Hatted Rooster Avatar answered Sep 13 '25 00:09

Hatted Rooster