So I often want to perform some STL algorithm on a range of elements and instead of a comparison function, I would like to pass a unary function f.
For example I would like to write something like this
std::max_element(begin(range), end(range), f);
to find the maximum element in the range after applying f. My current workaround looks something like that:
std::max_element(begin(range), end(range, [&f](auto a, auto b){ return f(a) < f(b); });
At first glance, this may look like no problem. But f could be a lambda expression itself or in another way more complicate than just f. I have two problem with that piece of code: a) It is error prone because one could accidently write f(a) < f(a) (especially if more complicated and one used copy and past). This is the problem of code duplication b) It does not express the intent very well. If I want to sort by a function, I do not want to deal with a comparison.
Unfortunately I have not found a good solution to this kind of problem in the standard library (or even boost), so I would like to ask you what your solution to this problem is. Is there any reason for the non-existence of this overload in the algorithms?