2011-03-03 60 views
4

可能重複:
Is std::list<>::sort stable?C++的std ::列表排序中保持秩序

不C++的std ::列表排序功能是保證保護列表中的相同元素的順序?例如。如果我們在列表中有對象A,B和C,並且比較運算符被重載以至於A == C和B,我們是否必須得到B-A-C或者是否有可能得到B-C-A?

+0

檢查http://stackoverflow.com/questions/1100561/stable- sort-a-stl-list-in-c。 – a1ex07 2011-03-03 20:02:30

回答

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. 
6

是的,標準要求list :: sort要穩定。