C++ for_each() output array elements

Description

C++ for_each() output array elements

#include <iostream>
#include <algorithm>
using namespace std;
void in_to_cm(double);     // declaration

int main()/* w  w  w. j  a va 2s  .co m*/
{                       // array of inches values
    double inches[] = { 3.5, 6.2, 1.0, 12.75, 4.33 };
    // output as centimeters
    for_each(inches, inches+5, in_to_cm);
    cout << endl;
    return 0;
}
void in_to_cm(double in)   // convert and display as centimeters
{
    cout << (in * 2.54) << ' ';
}



PreviousNext

Related