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

template<typename Iter>
Iter find_nearest(Iter begin, Iter end,
                  const typename std::iterator_traits<Iter>::value_type & value)
{
    typedef typename std::iterator_traits<Iter>::value_type T;
    return std::min_element(begin, end, [&value](const T& a, const T& b){
        return abs(value - a) < abs(value - b);
    });
}

int main() {   
    std::vector<int> l = {1, 43, 10, 17};
    auto a = find_nearest(l.begin(), l.end(), 42);
    std::cout << *a << std::endl;
}