foreach loops c++ code example
Example 1: for each c++
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function fn)
{
while (first!=last) {
fn (*first);
++first;
}
return fn; // or, since C++11: return move(fn);
}
Example 2: for each loop in c++
#include <iostream>
using namespace std
int main()
{
int arr[] = { 10, 20, 30, 40 };
// Printing elements of an array using
// foreach loop
for (int x : arr)
cout << x << endl;
}