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

Viewed 298

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().

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.

Related