#include <iostream>
#include <vector>
#include <algorithm>

template<typename Iter, typename Function>
Iter argmax(Iter begin, Iter end, Function f)
{
    typedef typename std::iterator_traits<Iter>::value_type T;
    return std::max_element(begin, end, [&f](const T& a, const T& b){
        return f(a) < f(b);
    });
}

int main() {
    std::vector<int> l({1, 43, 10, 17});
    auto a = argmax(l.begin(), l.end(), [](int x) { return -1 * abs(42 - x); });
    std::cout << *a << std::endl;
}