Reputation: 75
Do Pointers which point to elements of a std::list remain valid if I call the sort() method over the list? Is there a guarantee for this (standard)? It seems to work under Visual Studio 2012.
Upvotes: 0
Views: 211
Reputation: 74028
From "Working Draft C++, 2012-11-02"
23.3.5.5 list operations [list.ops]
void sort();
template <class Compare> void sort(Compare comp);
28 Requires:operator<
(for the first version) or comp (for the second version) shall define a strict weak ordering (25.4).
29 Effects: Sorts the list according to theoperator<
or aCompare
function object. Does not affect the validity of iterators and references.
30 Remarks: Stable.
31 Complexity: Approximately N log(N ) comparisons, whereN == size()
.
Upvotes: 5
Reputation: 13925
Sorting does not move elements of the list in the memory, only changes the next and previous link pointers. It should be fine.
Upvotes: 2