Sort and reverse an int array : reverse « STL Algorithms Modifying sequence operations « C++






Sort and reverse an int array

  
#include <algorithm>
#include <iostream>
#include <iterator>

using namespace std;

int main( )
{
   const int num_costs = 4;
   const float cost[num_costs] = { 4.77, 6.99, 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, "    " ) );
}
  
    
  








Related examples in the same category

1.Use reverse to reverse the order of elements
2.Use reverse to reverse order from second to last element but one
3.Using the STL generic reverse algorithm with a vector
4.Reverse the order of the found element with value 3 and all following elements
5.Reverse a sequence