1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
{
std::vector<int> v1( 10 );
std::iota( v1.begin(), v1.end(), 0 );
std::random_shuffle( v1.begin(), v1.end() );
std::copy( v1.begin(), v1.end(), std::ostream_iterator<int>( std::cout, " " ) );
std::cout << std::endl;
std::vector<int> v2( 20 );
std::iota( v2.begin(), std::next( v2.begin(), 10 ), 0 );
std::iota( std::next( v2.begin(), 10 ), v2.end(), 0 );
std::random_shuffle( v2.begin(), v2.end() );
std::copy( v2.begin(), v2.end(), std::ostream_iterator<int>( std::cout, " " ) );
std::cout << std::endl;
std::vector<int> v3( std::max( v1.size(), v2.size() ) );
std::copy( v1.begin(), v1.end(), v3.begin() );
int overflow = 0;
std::transform( v3.begin(), v3.end(), v2.begin(),
v3.begin(),
[&] ( int x, int y ) -> int
{
int digit = x + y + overflow;
overflow = ( digit > 9 ) ? 1 : 0;
digit %= 10;
return ( digit );
} );
if ( overflow ) v3.push_back( overflow );
std::copy( v3.begin(), v3.end(), std::ostream_iterator<int>( std::cout, " " ) );
std::cout << std::endl;
}
|