A possible implementation of for_each[1]

template<class InputIt, class UnaryFunction>
constexpr UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f)
{
for (; first != last; ++first) {
f(*first);
}
return f; // implicit move since C++11
}

Example[2]

// next example
#include <iostream> // std::cout
#include <iterator> // std::next
#include <list> // std::list
#include <algorithm> // std::for_each

int main () {
std::list<int> mylist;
for (int i=0; i<10; i++) mylist.push_back (i*10);

std::cout << "mylist:";
std::for_each (mylist.begin(),
std::next(mylist.begin(),5),
[](int x) {std::cout << ' ' << x;} );

std::cout << '\n';

return 0;
}
mylist: 0 10 20 30 40

Reference

Data Scientist/MLE/SWE @takemobi