C++ STL algorithm, for_each() code example

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the C++ for_each() to apply a specified function object to each element in a forward order within a range and returns the function object in C++ programming

To show: How to use the C++ algorithm, for_each() to apply a specified function object to each element in a forward order within a range and returns the function object in C++ programming

// C++ STL algorithm, for_each()

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

// the function object multiplies an element by a Factor

template <class Type>

class MultValue

{

private:

// the value to multiply by

Type Factor;

public:

// constructor initializes the value to multiply by

MultValue(const Type& _Val) : Factor(_Val) {}

// the function call for the element to be multiplied

voidoperator()(Type& elem) const

{ elem *= Factor; }

};

// the function object to determine the average

class Average

{

private:

// the number of elements

long num;

// the sum of the elements

long sum;

public:

// constructor initializes the value to multiply by

Average() : num(0), sum(0){}

// the function call to process the next element

void operator()( int elem )

{

// increment the element count

num++;

// add the value to the partial sum

sum += elem;

}

// return Average

operator double()

{

return (static_cast <double> (sum))/(static_cast <double> (num));

}

};

int main(void)

{

// vector container

vector <int> vec;

// vector iterator

vector <int>::iterator Iter1;

int i;

// pushing data, constructing the vec vector

for(i = -3; i <= 4; i++)

vec.push_back(i);

// print the data

cout<<"vec vector data: ";

for(Iter1 = vec.begin(); Iter1 != vec.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

// using for_each() to multiply each element by a Factor

for_each(vec.begin(), vec.end(), MultValue<int>(-2));

// print the result

cout<<"\nMultiplying the elements of the vec vector by the factor -2 gives: ";

for(Iter1 = vec.begin(); Iter1 != vec.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

// the function object is templatized and so can be used again on the elements with a different Factor

for_each(vec.begin(), vec.end(), MultValue<int>(5));

cout<<"\nMultiplying the elements of the vec vector by the factor 5 gives: ";

for(Iter1 = vec.begin(); Iter1 != vec.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

// the local state of a function object can accumulate information about a sequence of actions that the

// return value can make available, here the Average

double avemod2 = for_each(vec.begin(), vec.end(), Average());

cout<<"\nThe average of the elements of vec is (Average(vec)) = "<<avemod2<<endl;

return 0;

}

Output examples:

vec vector data: -3 -2 -1 0 1 2 3 4

Multiplying the elements of the vec vector by the factor -2 gives: 6 4 2 0 -2 -4 -6 -8

Multiplying the elements of the vec vector by the factor 5 gives: 30 20 10 0 -10 -20 -30 -40

The average of the elements of vec is (Average(vec)) = -5

Press any key to continue . . .