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?