#include <iostream>
using std::cout;
using std::endl;
#include <algorithm>
#include <numeric>
#include <vector>
#include <iterator>
int calculateCube( int );
int main()
{
std::ostream_iterator< int > output( cout, " " );
int a2[ 10 ] = { 100, 2, 8, 1, 50, 3, 8, 8, 9, 10 };
std::vector< int > v2( a2, a2 + 10 ); // copy of a2
cout << "Vector v2 contains: ";
std::copy( v2.begin(), v2.end(), output );
std::vector< int > cubes( 10 ); // instantiate vector cubes
// calculate cube of each element in v; place results in cubes
std::transform( v2.begin(), v2.end(), cubes.begin(), calculateCube );
cout << "\n\nThe cube of every integer in Vector v is:\n";
std::copy( cubes.begin(), cubes.end(), output );
cout << endl;
return 0;
}
int calculateCube( int value )
{
return value * value * value;
}
/*
Vector v2 contains: 100 2 8 1 50 3 8 8 9 10
The cube of every integer in Vector v is:
1000000 8 512 1 125000 27 512 512 729 1000
*/
|