Results 1 to 5 of 5

Thread: How to use the max_element() in C++

  1. #1
    Join Date
    Dec 2009
    Posts
    28

    How to use the max_element() in C++

    Hi, How are you All. I am having one query related to the functions of the C++ language. I have to submit the assignment on the yesterday in my college on max_element() function. Thus, I can not know much more about the max_element() function of the C++ language. So, I want to know about the max_element() function of the C++ language and how to use the max_element() function in the C++.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Use the max_element() in C++

    The max_element() function can be used to return an iterator that can be pointing to an element with the maximum value in the given specified range as [fst,lst). The comparisons can be performed using either an less than operator "<" for the first version or cmp variable for the second version. The behavior of the max_element() function template can be similar to :
    Code:
    template <class FrwrdItrtr>
      FrwrdItrtr max_element ( FrwrdItrtr fst, FrwrdItrtr lst )
    {
      FrwrdItrtr lrgst = fst;
      if (fst==lst) return lst;
      while (++fst!=lst)
        if (*lrgst<*fst)    
          lrgst=fst;
      return lrgst;
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    How to use the max_element() in C++

    The program that can be mentioned below illustrates you about how to use the max_element() function in the C++ language programming :
    Code:
    #include <vector>
    #include <set>
    #include <algorithm>
    #include <iostream>
    #include <ostream>
    using namespace std;
    class Cnt;
    ostream& operator<<( ostream& osn, const Cnt& rs );
    class Cnt
    {
    public:
    Cnt( int p = 0 ) : m_nVl( p )
    {
    }
    Cnt( const Cnt& rs ) : m_nVl( rs.m_nVl )
    {
    }
    Cnt& operator=( const Cnt& rs ) 
    {
    m_nVl = rs.m_nVl; 
    return *this;
    }
    bool operator<( const Cnt& rs ) const 
    {
    return ( m_nVl < rs.m_nVl );
    }
    friend ostream& operator<<( ostream& osn, const Cnt& rs );
    private:
       int m_nVl;
    };
    inline ostream& operator<<(ostream& osn, const Cnt& rs)
    {
       osn << "Cnt( " << rs.m_nVl << " )"; 
       return osn;
    }
    bool mod_lesser ( int elm1, int elm2 )
    {
       if ( elm1 < 0 ) 
          elm1 = - elm1;
       if ( elm2 < 0 ) 
          elm2 = - elm2;
       return elm1 < elm2;
    };
    int main( )
    {
       Cnt d1 = 1, d2 = 2, d3 = -3;
       set<Cnt> t1;
       set<Cnt>::iterator t1_Itr, t1_R1_Itr, t1_R2_Itr;
       t1.insert ( d1 );
       t1.insert ( d2 );
       t1.insert ( d3 );
       cout << "t1 = (";
       for ( t1_Itr = t1.begin( ); t1_Itr != --t1.end( ); t1_Itr++ )
          cout << " " << *t1_Itr << ",";
       t1_Itr = --t1.end( );
       cout << " " << *t1_Itr << " )." << endl;
       t1_R1_Itr = max_element ( t1.begin ( ) , t1.end ( ) );
       cout << "The largest element in t1 is: " << *t1_R1_Itr << endl;
       cout << endl;
       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
    Code:
    t1 = ( Cnt( -3 ), Cnt( 1 ), Cnt( 2 ) ).
    The largest element in t1 is: Cnt( 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

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    The max_element() in C++

    The max_element() function can be used to return maximum of an element that can be in the given specified range.The following can be the general syntax of the max_element() function of the C++language :
    Code:
    template <class FrwrdItrtr>
      FrwrdItrtr max_element ( FrwrdItrtr fst, FrwrdItrtr lst );
    template <class FrwrdItrtr, class Cmpr>
      FrwrdItrtr max_element ( FrwrdItrtr fst, FrwrdItrtr lst,
                                    Compare cmp );

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    How to use the max_element()

    The max_element() function of the C++ language can returns an iterator to the largest an element in the given specified range as [strt,nd). If the variable of the predicate of the binary can be given then the max_element() function can be used in place of the less than < operator to determine the maximum an element among the given element. I hope you should understands from the above explanation.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,827,829.31531 seconds with 15 queries