C ++ std :: list sort keep order

Possible duplicate:
Is std :: list <gt; :: sort stable?

Is the std :: list sorting function stored in C ++ to keep the order of equal items in the list? For instance. if we have objects A, B and C in the list, and the comparison operators are overloaded so that A == C and B <A, will we definitely get a BAC or can we get a BCA?

+4
source share
2 answers

Yes, in C ++ list::sort() is stable according to ISO 14882: 2003 23.2.2.4 [lib.list.ops] / 31

 Effects: Sorts the list according to the operator< or a Compare function object. Notes: Stable: the relative order of the equivalent elements is preserved. If an exception is thrown the order of the elements in the list is indeterminate. 
+11
source

Yes, the standard requires list :: sort to be stable.

+6
source

Source: https://habr.com/ru/post/1342180/


All Articles