for_each — algorithm. range-based for — language feature.
std::for_each:
1std::vector<int> v = {1, 2, 3};23// Lambda4std::for_each(v.begin(), v.end(),5 [](int& x) { x *= 2; }6);78// Function object9struct Double {10 void operator()(int& x) const { x *= 2; }11};12std::for_each(v.begin(), v.end(), Double());
Range-based for:
1for (int& x : v) {2 x *= 2;3}
Key differences: