fork(3) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. template<typename Iter, typename Function>
  6. Iter argmax(Iter begin, Iter end, Function f)
  7. {
  8. typedef typename std::iterator_traits<Iter>::value_type T;
  9. return std::max_element(begin, end, [&f](const T& a, const T& b){
  10. return f(a) < f(b);
  11. });
  12. }
  13.  
  14. int main() {
  15. std::vector<int> l({1, 43, 10, 17});
  16. auto a = argmax(l.begin(), l.end(), [](int x) { return -1 * abs(42 - x); });
  17. std::cout << *a << std::endl;
  18. }
Success #stdin #stdout 0s 3060KB
stdin
Standard input is empty
stdout
43