【发布时间】:2011-03-03 19:56:48
【问题描述】:
C++ std::list 排序函数是否保证保留列表中相等元素的顺序?例如。如果我们在列表中有对象 A、B 和 C,并且比较运算符被重载,使得 A == C 和 B
【问题讨论】:
C++ std::list 排序函数是否保证保留列表中相等元素的顺序?例如。如果我们在列表中有对象 A、B 和 C,并且比较运算符被重载,使得 A == C 和 B
【问题讨论】:
是的,在 C++ 中,list::sort() 是稳定的,符合 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.
【讨论】:
是的,标准要求 list::sort 是稳定的。
【讨论】: