Search code examples
c++algorithmc++11vector

What is this version of sort() which takes a container instead of two iterators?


I was reading Stroustrup's blog on c++ (http://isocpp.org/blog/2014/12/myths-3) when I found an intersting piece of code:

void do_my_sort(vector<double>& v)
{
  sort(v,[](double x, double y) { return x>y; });  // sort v in decreasing order
}

int main()
{
  vector<double> vd;
  // ... fill vd ...
  do_my_sort(v);
  // ...
} 

Notice that the sort does not use the traditional sort(v.begin(), v.end(), ...) which Stroustrup explains:

I used a container version of sort() to avoid being explicit about the iterators.

However, I tried the same code on my C++11 compiler but it fails to compile. I also tried the same on a C++14 compiler using ideone but it too fails to compile, saying that there is no matching call to sort.

Why is this?

Also, Stroustrup next mentions:

I could go further and use a C++14 comparison object:

sort(v,greater<>()); // sort v in decreasing order

I have used comparators like great<>() for sort in C++11 also. Why is he stating that this is a C++14 comparison object?


Solution

  • He wrote that himself, it is not standard. Thus you cannot find it in the standard library. You could implement it like this:

    template <class Container, class Comp>
    void sort (Container& cont, Comp comp) {
        using std::begin;
        using std::end;
        std::sort(begin(cont), end(cont), comp);
    }
    

    As Clukester pointed out, there is also boost::sort that offers this functionality.