Yggi
Yggi

Reputation: 75

std::list::sort and pointers to elements

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

Answers (2)

Olaf Dietsche
Olaf Dietsche

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 the operator< or a Compare function object. Does not affect the validity of iterators and references.
30 Remarks: Stable.
31 Complexity: Approximately N log(N ) comparisons, where N == size().

Upvotes: 5

Matzi
Matzi

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

Related Questions