c ++ std :: list sort保留顺序

时间:2011-03-03 19:56:49

标签: c++ sorting stdlist

  

可能重复:
  Is std::list<>::sort stable?

C ++ std :: list sort函数是否保证保留列表中相等元素的顺序?例如。如果我们在列表中有对象A,B和C并且比较运算符被重载,那么A == C和B&lt; A,我们必须得到B-A-C还是可以获得B-C-A?

2 个答案:

答案 0 :(得分:11)

是的,在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.

答案 1 :(得分:6)

是的,标准要求list :: sort保持稳定。