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.
std::list::sort. Sorts the elements in ascending order. The order of equal elements is preserved.
The C++ function std::list::sort() sorts the elements of the list in ascending order. The order of equal elements is preserved.
std::sort requires random access iterators and so cannot be used with list .
Consider using std::list if: You need to store many items but the number is unknown. You need to insert or remove new elements from any position in the sequence. You do not need efficient access to random elements.
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.
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