max_element

查找最大的组件第一次出现在该排序的标准可以由二进制谓词指定的指定的大小。

template<class ForwardIterator>
   ForwardIterator max_element(
      ForwardIterator _First, 
      ForwardIterator _Last
   );
template<class ForwardIterator, class BinaryPredicate>
   ForwardIterator max_element(
      ForwardIterator _First, 
      ForwardIterator _Last, 
      BinaryPredicate _Comp
   );

参数

  • _First
    解决仅向前迭代器的第一个元素的位置。最大的元素将搜索范围。

  • _Last
    解决仅向前的迭代器通过最终元素的位置一个位于最大的元素将搜索范围。

  • _Comp
    定义含义的用户定义的谓词函数对象哪个元素比另一个大。二进制谓词采用两个参数,并应返回 true ,当第一个元素比否则第二个元素和 false 小于时。

返回值

解决仅向前的迭代器最大元素的第一次出现的位置在搜索的范围。

备注

该范围引用必须是有效的;所有指针必须dereferenceable,在每个序列中最后位置以访问按增量。

复杂的线性:(_Last – _First) – 1次比较对于非空范围是必需的。

示例

// alg_max_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 greater 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 maximum 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 = max_element ( s1.begin ( ) , s1.end ( ) );

   cout << "The largest 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 = max_element ( v1.begin ( ) , v1.end ( ) );
   v1_R2_Iter = max_element ( v1.begin ( ) , v1.end ( ), mod_lesser);

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

Output

s1 = ( CInt( -3 ), CInt( 1 ), CInt( 2 ) ).
The largest element in s1 is: CInt( 2 )

Vector v1 is ( 0 1 2 3 -2 -4 -6 -8 ).
The largest element in v1 is: 3
The largest element in v1 under the mod_lesser
 binary predicate is: -8

要求

标头: <algorithm>

命名空间: std

请参见

参考

Nonpredicate Version of max_element

Predicate Version of max_element

标准模板库