C++ for_each Loop

Apply Function to Range

for_each loop applies a function to the range of elements of a collection. In other words each element from the collection will be passed to the function as a parameter and function will be executed the number of times elements present in a collection. This loop is defined in a algorithm header so have to include this header in your program to use for_each


#include 

Below is the syntax of for_each loop

Syntax of for_each


for_each (InputIterator first, InputIterator last, Function fn)

for_each applies function fn for each element in the range starting from first to last.


#include 
#include 
using namespace std;

void fun1(int x) 
{
    cout << x << " ";
}

struct Class1 // object type function
{           
    void operator() (int x) 
    {
        cout << x << " ";
    }
} obj1;


int main() 
{
    int arr[] = {11, 21, 4, 13};
    for_each(arr, arr + 4, fun1);
    cout << endl;
  	for_each(arr, arr + 4, obj1);
    return 0;
}

The output of above code will be

11  21  4  13
11  21  4  13

Above code can also be written using vector as below


#include 
#include 
using namespace std;

void fun1(int x) 
{
    cout << x << " ";
}

struct Class1 // object type function
{           
    void operator() (int x) 
    {
        cout << x << " ";
    }
} obj1;


int main() 
{
    vector v1;
    v1.push_back(11);
    v1.push_back(23);
    v1.push_back(4);
    v1.push_back(13);
    for_each(v1.begin(), v1.end(), fun1);        
    cout << endl;
	for_each(v1.begin(), v1.end(), obj1);        
    return 0;
}

Exception in for_each

If the function throws an exception then for_each will also throw the same exception and will break/terminate the loop. Below is an example for the exception case.


#include 
#include 
using namespace std;

void fun1(int x) 
{
    cout << x << " ";
    if (x % 2 == 0)
    {
        throw 100;
    }
}

struct Class1 // object type function
{           
    void operator() (int x) 
    {
        cout << x << " ";
        if (x % 2 == 0)
        {
            throw 100;
        }            
    }
} myobject;

int main() 
{
    vector v1;
    v1.push_back(11);
    v1.push_back(23);
    v1.push_back(4);
    v1.push_back(13);

    try 
    {
        for_each(v1.begin(), v1.end(), fun1);        
    } 
    catch (int i) 
    {
        cout << endl << "Got exception...Value thrown is " << i << endl;
    }
    try 
    {
        for_each (v1.begin(), v1.end(), myobject);
    } 
    catch (int i) 
    {
        cout << endl << "Got exception...Value thrown is " << i << endl;
    }    
    return 0;
}

Output of above code will be

11  23  4
Got exception...Value thrown is 100
11  23  4
Got exception...Value thrown is 100

Advantages of for_each loop

  1. Allows us to write an algorithm on top of for_each that works with any iterator.
  2. Silly typing bugs can be reduced.
  3. for_each is more generic than 'for loop' as we can use it to iterate over any type of container.
  4. for_each makes more readable code.
  5. Performance: Code containing for_each are more efficient.

 


Next > < Prev
Scroll to Top