Why does vector not have sort() method as a member function of vector, while list does?

前端 未结 6 1503
感动是毒
感动是毒 2021-01-11 09:29

There is a sort() method for lists in STL. Which is absurd, because I would be more inclined to sort an array/vector. Why isn\'t sort() provided for vector? Is there some un

6条回答
  •  没有蜡笔的小新
    2021-01-11 10:19

    std::sort() in does sorting on containers with random access iterators like std::vector.

    There is also std::stable_sort().

    edit - why does std::list have its own sort() function versus std::vector?

    std::list is different from both std::vector and std::deque (both random access iterable) in how it's implemented, so it contains its own sort algorithm that is specialized for its implementation.

提交回复
热议问题