In C++,
There are four types of loops:
-for loop
-while loop
-do while loop
-foreach loop
Foreach loop:
-Apart from other loop this loop is special kind of functionality.
-This loop is not present in C language.
-For each loop are used to iterate the sequence or collection like vectors.
Syntax-
for_each (InputIterator first, InputIterator last, Function fn)
program:
#include< iostream>
#include< algorithm>
using namespace std;
int func(int i)
{
cout<< i<
int main()
{
int a[5]={1,2,3,4,5};
for_each(a,a+5,func);
return 0;
}
1
2
3
4
5