Finding the Minimum and Maximum of a Data Type : min « STL Algorithms Min Max « C++






Finding the Minimum and Maximum of a Data Type

  
#include <algorithm>
#include <limits>
#include <numeric>
#include <vector>

#include <iostream>

using namespace std;

template <class T>
void print(T& c){
   for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
      std::cout << *i << endl;
   }
}

int main( )
{
   const int n = 8;
   vector<unsigned char> pixels1( n, 250 );
   vector<int> int_pixels( pixels1.begin(), pixels1.end() );
   print( int_pixels);

   // create and display the second group of pixels
   vector<unsigned char> pixels2( n, 1 );
   partial_sum( pixels2.begin(), pixels2.end(), pixels2.begin() );
   int_pixels.assign( pixels2.begin(), pixels2.end() );
   print( int_pixels);

   // perform and display sum with wrap-around
   vector<unsigned char> sum( n );
   transform( pixels1.begin(), pixels1.end(), pixels2.begin(),sum.begin(), plus<unsigned char>() );
   int_pixels.assign( sum.begin(), sum.end() );
   print( int_pixels);

}
  
    
  








Related examples in the same category

1.Use min for char and integer
2.Call min() with special comparison function
3.Get min and max value from a vector with your won logic