C++ Array iterate by for_each()

Description

C++ Array iterate by for_each()

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

int main()//from w  w w .  j  ava  2  s.  c  o  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