0

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.

2 Answers 2

5

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().

Sign up to request clarification or add additional context in comments.

Comments

2

Sorting does not move elements of the list in the memory, only changes the next and previous link pointers. It should be fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.