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
    작은 요소에 대해 검색할 주소 범위의 첫 번째 요소의 위치는 정방향 반복기입니다.

  • last
    작은 요소에 대해 검색할 범위의 마지막 요소를 지나서 위치 주소는 정방향 반복기입니다.

  • comp
    요소가 다른 보다 큰 하는 것이 정의 조건자 함수의 사용자 정의 개체입니다.이진 술 부 두 개의 인수를 사용 하 고 반환 해야 true 두 번째 요소의 첫 번째 요소인 미만 및 거짓 그렇지.

반환 값

정방향 반복기 주소 범위에서 가장 작은 요소가 맨 앞의 위치를 검색 합니다.

설명

참조 범위 유효 해야 합니다. 모든 포인터는 dereferenceable 이어야 하며 각 시퀀스에서 마지막 위치에서 첫 번째 도달할 수 여 증분 합니다.

복잡 한 선형입니다: (last - first)-1 비교는 비어 있지 않은 범위에 대 한 필요.

예제

// 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 Samples)

Predicate Version of min_element

표준 템플릿 라이브러리