sort() function in C++

c++

Solution

`std::sort` is a function template that works with any pair of random-access iterators. Consequently, the algorithm implemented by `std::sort` is tailored (optimized) for random access. Most of the time it is going to be some flavor of quick-sort.

`std::list::sort` is a dedicated list-oriented version of sort. `std::list` is a container that does not support [efficient] random access, which means that you can't use `std::sort` with it. This creates the need for a dedicated sorting algorithm. Most of the time it will be implemented as some flavor of merge-sort.

Problem

I found two forms of sort() in C++: 1) sort(begin, end); 2) XXX.sort(); One can be used directly without an object, and one is working with an object. Is that all? What's the differences between these two sort()? They are from the same library or not? Is the second a method of XXX? Can I use it like this ``` vector<int> myvector myvector.sort(); ``` or ``` list<int> mylist; mylist.sort(); ```

Original source