Practice mode C++

Why can std::sort NOT be used directly on a std::list?

Correct answer A. std::sort needs random-access iterators but std::list provides only bidirectional iterators

Explanation

std::sort relies on jumping to arbitrary positions in constant time, so it requires random-access iterators, which vector, deque and plain arrays provide. A doubly linked list only supports stepping forwards and backwards. That is why std::list has its own member function list.sort(), a merge sort that relinks nodes instead of moving elements.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.