Mega Code Archive

 
Categories / C++ Tutorial / STL Algorithms Non Modifying Sequence Operations
 

The square of every integer in a vector

#include <iostream> #include <algorithm> #include <numeric> #include <vector> using namespace std; void outputSquare( int ); int main(){    const int SIZE = 10;    int a1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };    vector< int > v( a1, a1 + SIZE );    random_shuffle( v.begin(), v.end() );    cout << "\n\nThe square of every integer in Vector v is:\n";    for_each( v.begin(), v.end(), outputSquare );    cout << endl;    return 0; } void outputSquare( int value ) { cout << value * value << ' '; }