共用方式為


min_element

尋找最小的項目第一個排序準則可能由二進位述詞指定的所指定的範圍內。

template<class ForwardIterator> 
   ForwardIterator min_element( 
      ForwardIterator first,  
      ForwardIterator last 
   ); 
template<class ForwardIterator, class BinaryPredicate> 
   ForwardIterator min_element( 
      ForwardIterator first,  
      ForwardIterator last,
      BinaryPredicate comp 
   );

參數

  • first
    解決向前 Iterator 的第一個項目位置範圍中搜尋最小的項目。

  • last
    解決的 Iterator 向前到最後的項目位置的範圍中搜尋最小的項目。

  • comp
    使用者定義的述詞函式物件,定義在此意義上某個項目大於另一個。 如果第一個項目則為時,小於第二個項目和 false 二進位述詞會採用兩個引數,則應該傳回 true

傳回值

解決向前的 Iterator 最小的元素的第一個項目的位置在搜尋的範圍。

備註

參考的已排序來源範圍必須是有效的。所有指標都必須是可取值的,而且每個序列中的最後一個位置是可從第一個位置透過遞增方式到達。

複雜線性:last – ( first) – 1 比較針對一個非 null 的範圍是必要的。

範例

// alg_min_element.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()
{
   // Searching a set container with elements of type CInt 
   // for the minimum element 
   CInt c1 = 1, c2 = 2, c3 = -3;
   set<CInt> s1;
   set<CInt>::iterator s1_Iter, s1_R1_Iter, s1_R2_Iter;
   
   s1.insert ( c1 );
   s1.insert ( c2 );
   s1.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;

   s1_R1_Iter = min_element ( s1.begin ( ) , s1.end ( ) );

   cout << "The smallest element in s1 is: " << *s1_R1_Iter << endl;
   cout << endl;

   // Searching a vector with elements of type int for the maximum
   // element under default less than & mod_lesser binary predicates
   vector <int> v1;
   vector <int>::iterator v1_Iter, v1_R1_Iter, v1_R2_Iter;

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

   int ii;
   for ( ii = 1 ; ii <= 4 ; ii++ )
   {
      v1.push_back( - 2 * ii );
   }
   
   cout << "Vector v1 is ( " ;
   for ( v1_Iter = v1.begin( ) ; v1_Iter != v1.end( ) ; v1_Iter++ )
      cout << *v1_Iter << " ";
   cout << ")." << endl;

   v1_R1_Iter = min_element ( v1.begin ( ) , v1.end ( ) );
   v1_R2_Iter = min_element ( v1.begin ( ) , v1.end ( ), mod_lesser);

   cout << "The smallest element in v1 is: " << *v1_R1_Iter << endl;
   cout << "The smallest element in v1 under the mod_lesser"
        << "\n binary predicate is: " << *v1_R2_Iter << endl;
}
  

需求

標頭:<algorithm>

命名空間: std

請參閱

參考

min_element (STL 範例)

min_element 的述詞版本

標準樣板程式庫