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

5

可能重复:
std::list<>::sort是否稳定?

C++的std::list sort函数保证在列表中保留相等元素的顺序吗?例如,如果列表中有对象A、B和C,并且比较运算符被重载以使A == C并且B < A,则我们一定会得到B-A-C,还是可能得到B-C-A?


请查看以下网址以了解如何在C++中对STL列表进行稳定排序:https://dev59.com/hkjSa4cB1Zd3GeqPEVfw。 - a1ex07
2个回答

12

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

6

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接