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

前端 未结 6 1510
感动是毒
感动是毒 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:05

    Answering the question of "Why?"

    A sorting algorithm for std::vector is the same as sorting a native array and is the same (probably) as sorting a custom vector class.

    The STL was designed to separate containers and algorithms, and to have an efficient mechanism for applying an algorithm to data that has the right characteristics.

    This lets you write a container that might have specific characteristics, and to get the algorithms free. Only where there is some special characteristic of the data that means the standard algorithm is unsuitable is a custom implementation supplied, as in the case of std::list.

提交回复
热议问题