【问题标题】:c++ std::list sort preserve order [duplicate]c ++ std :: list排序保留顺序[重复]
【发布时间】:2011-03-03 19:56:48
【问题描述】:

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

C++ std::list 排序函数是否保证保留列表中相等元素的顺序?例如。如果我们在列表中有对象 A、B 和 C,并且比较运算符被重载,使得 A == C 和 B

【问题讨论】:

标签: c++ sorting stdlist


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2018-08-10
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      • 2017-03-17
      • 2016-06-26
      • 2016-12-28
      • 1970-01-01
      • 2018-02-09
      相关资源
      最近更新 更多