Is std::list order guaranteed?

Is the order of the elements within an std::list guaranteed to remain in-order (unless, of course, a sort or something occurs)?

Moreover, is there any potentially undefined behavior with lists that might jumble them up?

I was/am under the impression that containers such as std::deque and the like are order-safe, but alas std::deque is not double-linked.

Upvotes: 6

Views: 7162

Answers (1)

john
john

Reputation: 87997

Yes the order is guaranteed in std::list. Since anything can happen with UB, mixing up the order of a std::list is possible (though unlikely I would think).

Short answer is that if your lists are not in the order you think they should be then the most likely reason is a bug in your program.

Upvotes: 7

Related Questions