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().
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()
returnstrue
.Complexity: Linear.
So no, you get no guarantees on the order of destruction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With