In C++17 you can do this:
#include <iostream>
#include <algorithm>
#include <functional>
int main()
{
double values[5] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
// Notice this:
std::sort(values, values+5, std::greater());
for(double v: values) std::cout << v << " ";
std::cout << "\n";
}
You don't actually need to specify the template parameter of std::greater
.
It will be automatically deduced as double
. That's really nice.
But wait... How?!?
There's nothing telling std::greater
that the template parameter should be
of type double
. It's not taking any constructor parameters or anything.
And the declaration of std::sort()
is ostensibly like this:
template<class RandomIt, class Compare>
void sort(RandomIt first, RandomIt last, Compare comp);
so there's nothing there telling it that it should be double either.
So how?