Mega Code Archive

 
Categories / C++ / Algorithm
 

Use copy to fill values to an array

#include <algorithm> #include <iostream> #include <iterator> using namespace std; int main( ) {    const int num_costs = 4;    const float cost[num_costs] = { 4.11, 6.77, 8.88, 9.22 };    copy( cost, cost + num_costs,ostream_iterator<float>( cout, "     " ) );    const char* fruit[] = { "A", "B", "C" };    copy( fruit, fruit + sizeof( fruit ) / sizeof( fruit[0] ),       ostream_iterator<const char*>( cout, "    " ) );    int year[] = { 2001, 2002, 2003, 2004, 2005 };    const int num_years = sizeof( year ) / sizeof( year[0] );    copy( year, year + num_years,ostream_iterator<int>( cout, "    " ) );    sort( year, year + num_years );    copy( year, year + num_years,ostream_iterator<int>( cout, "    " ) );    reverse( year, year + num_years );    copy( year, year + num_years,ostream_iterator<int>( cout, "    " ) ); }