Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 06-03-2010
Member
 
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++.
Reply With Quote
  #2  
Old 06-03-2010
Member
 
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;
}
Reply With Quote
  #3  
Old 06-03-2010
Member
 
Join Date: May 2008
Posts: 2,008
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
Reply With Quote
  #4  
Old 06-03-2010
Member
 
Join Date: Apr 2008
Posts: 2,001
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 );
Reply With Quote
  #5  
Old 06-03-2010
Member
 
Join Date: May 2008
Posts: 2,293
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search



All times are GMT +5.5. The time now is 06:28 PM.