C++/STL Algorithms Min Max/min

Материал из C\C++ эксперт
Версия от 10:27, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Call min() with special comparison function

  
 
/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <algorithm>
#include <iostream>
using namespace std;
/* function that compares two pointers by comparing the values to which they po
int
 */
bool int_ptr_less (int* a, int* b)
{
    return *a < *b;
}
int main()
{
    int x = 17;
    int y = 42;
    int* px = &x;
    int* py = &y;
    int* pmin;
    // call min() with special comparison function
    pmin = min (px, py, int_ptr_less);
    cout << *pmin;
    //...
}
 /* 
17
 */


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);
}


Get min and max value from a vector with your won logic

  
#include <algorithm>
#include <functional>
#include <vector>
#include <iostream>
using namespace std;
class MinAndMax : public unary_function<int, void>
{
public:
  MinAndMax();
  void operator()(int elem);
  int min, max;
protected:
  bool first;
};
MinAndMax::MinAndMax() : min(-1), max(-1), first(true){}
void MinAndMax::operator()(int elem){
  if (first) {
    min = max = elem;
  } else if (elem < min) {
    min = elem;
  } else if (elem > max) {
    max = elem;
  }
  first = false;
}
int main(int argc, char** argv){
  vector<int> myVector;
  myVector.push_back(1);
  myVector.push_back(2);
  myVector.push_back(3);
  MinAndMax func;
  func = for_each(myVector.begin(), myVector.end(), func);
  cout << "The max is " << func.max << endl;
  cout << "The min is " << func.min << endl;
  return (0);
}


Use min for char and integer

  
 
#include <iostream>
using std::cout;
using std::endl;
#include <algorithm>
int main()
{
   cout << "\nThe minimum of "G" and "Z" is: " << std::min( "G", "Z" );
   cout << "\nThe minimum of 12 and 7 is: " << std::min( 12, 7 );
   cout << endl; 
   return 0;
}
/* 
The minimum of "G" and "Z" is: G
The minimum of 12 and 7 is: 7
 */