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