min

比較兩個物件並傳回較小,其中兩個排序準則可能是由二進位述詞指定。

template<class Type>
   const Type& min(
      const Type& _Left, 
      const Type& _Right
   );
template<class Type, class Pr>
   const Type& min(
      const Type& _Left, 
      const Type& _Right,
      BinaryPredicate _Comp
   );

參數

  • _Left
    要比較的第一個物件。

  • _Right
    要比較的第二個兩個物件。

  • _Comp
    用於的二進位述詞比較兩個物件。

傳回值

減去兩個物件,除非沒有小於,在此情況下,會傳回第一個物件的情況下。

備註

min 演算法的特殊之處在於會做為參數傳遞的物件。大部分的標準樣板程式庫 (STL) 演算法在位置會當做參數傳遞的 Iterator 所指定項目的範圍。

範例

// alg_min.cpp
// compile with: /EHsc
#include <vector>
#include <set>
#include <algorithm>
#include <iostream>
#include <ostream>

using namespace std;
class CInt;
ostream& operator<<( ostream& osIn, const CInt& rhs );

class CInt
{
public:
   CInt( int n = 0 ) : m_nVal( n ){}
   CInt( const CInt& rhs ) : m_nVal( rhs.m_nVal ){}
   CInt& operator=( const CInt& rhs ) {m_nVal = 
   rhs.m_nVal; return *this;}
   bool operator<( const CInt& rhs ) const 
      {return ( m_nVal < rhs.m_nVal );}
   friend ostream& operator<<(ostream& osIn, const CInt& rhs);

private:
   int m_nVal;
};

inline ostream& operator<<( ostream& osIn, const CInt& rhs )
{
   osIn << "CInt( " << rhs.m_nVal << " )"; 
   return osIn;
}

// Return whether modulus of elem1 is less than modulus of elem2
bool mod_lesser ( int elem1, int elem2 )
{
   if ( elem1 < 0 ) 
      elem1 = - elem1;
   if ( elem2 < 0 ) 
      elem2 = - elem2;
   return elem1 < elem2;
};

int main( )
{
   // Comparing integers directly using the min algorithm with
   // binary predicate mod_lesser & with default less than
   int a = 6, b = -7, c = 7 ;
   const int& result1 = min ( a, b, mod_lesser );
   const int& result2 = min ( b, c );

   cout << "The mod_lesser of the integers 6 & -7 is: " 
        << result1 << "." << endl;
   cout << "The lesser of the integers -7 & 7 is: " 
        << result2 << "." << endl;
   cout << endl;

   // Comparing set containers with elements of type CInt 
   // using the min algorithm
   CInt c1 = 1, c2 = 2, c3 = 3;
   set<CInt> s1, s2, s3;
   set<CInt>::iterator s1_Iter, s2_Iter, s3_Iter;
   
   s1.insert ( c1 );
   s1.insert ( c2 );
   s2.insert ( c2 );
   s2.insert ( c3 );

   cout << "s1 = (";
   for ( s1_Iter = s1.begin( ); s1_Iter != --s1.end( ); s1_Iter++ )
      cout << " " << *s1_Iter << ",";
   s1_Iter = --s1.end( );
   cout << " " << *s1_Iter << " )." << endl;

   cout << "s2 = (";
   for ( s2_Iter = s2.begin( ); s2_Iter != --s2.end( ); s2_Iter++ )
      cout << " " << *s2_Iter << ",";
   s2_Iter = --s2.end( );
   cout << " " << *s2_Iter << " )." << endl;

   s3 = min ( s1, s2 );
   cout << "s3 = min ( s1, s2 ) = (";
   for ( s3_Iter = s3.begin( ); s3_Iter != --s3.end( ); s3_Iter++ )
      cout << " " << *s3_Iter << ",";
   s3_Iter = --s3.end( );
   cout << " " << *s3_Iter << " )." << endl << endl;

   // Comparing vectors with integer elements using min algorithm
   vector <int> v1, v2, v3, v4, v5;
   vector <int>::iterator Iter1, Iter2, Iter3, Iter4, Iter5;

   int i;
   for ( i = 0 ; i <= 2 ; i++ )
   {
      v1.push_back( i );
   }

   int ii;
   for ( ii = 0 ; ii <= 2 ; ii++ )
   {
      v2.push_back( ii );
   }

   int iii;
   for ( iii = 0 ; iii <= 2 ; iii++ )
   {
      v3.push_back( 2 * iii );
   }
   
   cout << "Vector v1 is ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;

   cout << "Vector v2 is ( " ;
   for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
      cout << *Iter2 << " ";
   cout << ")." << endl;
   
   cout << "Vector v3 is ( " ;
   for ( Iter3 = v3.begin( ) ; Iter3 != v3.end( ) ; Iter3++ )
      cout << *Iter3 << " ";
   cout << ")." << endl;

   v4 = min ( v1, v2 );
   v5 = min ( v1, v3 );

   cout << "Vector v4 = min ( v1,v2 ) is ( " ;
   for ( Iter4 = v4.begin( ) ; Iter4 != v4.end( ) ; Iter4++ )
      cout << *Iter4 << " ";
   cout << ")." << endl;

   cout << "Vector v5 = min ( v1,v3 ) is ( " ;
   for ( Iter5 = v5.begin( ) ; Iter5 != v5.end( ) ; Iter5++ )
      cout << *Iter5 << " ";
   cout << ")." << endl;
}
  
  
  
  
  
  
  
  
  
  

需求

標題: <algorithm>

命名空間: std

請參閱

參考

標準樣板程式庫