4.9
Copied!
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
inline ostream& operator <<( ostream& out, const vector< unsigned > & obj ) {
cout << "< ";
for( auto& p: obj )
cout << p << " ";
return out << ">";
}
void pow2( unsigned& i ) { i *= i; }
int main( void ) {
const int examples = 4;
for( int i = 0; i < examples; i ) {
unsigned ai[] = { 1, 2, 3, 4 , 5, 6, 7, 8, 9 },
ni = sizeof( ai ) / sizeof( ai[ 0 ] );
vector< unsigned > vi( ai, ai ni );
cout << vi;
switch( i ) {
case 0:
for_each( vi.begin(), vi.end(), pow2 );
cout << " => " << vi << endl;
break;
case 1:
for_each( ai, ai ni, pow2 );
cout << " => " << vector< unsigned >( ai, ai ni ) << endl;
break;
case 2:
for( auto& i : ai ) pow2( i );
cout << " => " << vector< unsigned >( ai, ai ni ) << endl;
break;
case 3:
for_each( vi.begin() 2, vi.end() - 2, pow2 );
cout << " => " << vi << endl;
break;
}
}
system("pause");
return 0;
}
Run on cpp.sh
Copied!