446,413 Members | 999 Online Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

# get the max "absolute" integer in a vector

 P: n/a Hi, Can someone provide me an example that uses std::max_element() (probablly the predicate version) to return the max "absolute" integer in a vector? Your help is much appreciated. Tony ps. // return the max integer in a vector std::vector::iterator p = std::max_element(m.begin(), m.end()); Mar 14 '07 #1
10 Replies

 P: n/a JDT wrote: Hi, Can someone provide me an example that uses std::max_element() (probablly the predicate version) to return the max "absolute" integer in a vector? Your help is much appreciated. Tony ps. // return the max integer in a vector std::vector::iterator p = std::max_element(m.begin(), m.end()); This is the traditional way. You can also attemp to make ComparisonOp into a template if you wish. ------------------------------------------------ #include #include #include class ComparisonOp : public std::binary_function { public: result_type operator()( const first_argument_type &a, const second_argument_type &b ) const { return (a < b); } }; int main() { std::vector::iterator p = std::max_element(m.begin(), m.end(), ComparisonOp()); } ---------------------------------------------------------- This uses boost lambda. ---------------------------------------------------------- #include #include #include // this works if operator< is defined for your type (int in your case) using namespace boost::lambda; int main() { std::vector::iterator p = std::max_element( m.begin(), m.end(), (_1 < _2) ); } ------------------------------------------------------------- Mar 14 '07 #2

 P: n/a Piyo wrote: JDT wrote: >Hi,Can someone provide me an example that uses std::max_element()(probablly the predicate version) to return the max "absolute" integerin a vector? Your help is much appreciated.Tonyps.// return the max integer in a vectorstd::vector::iterator p = std::max_element(m.begin(), m.end()); This is the traditional way. You can also attemp to make ComparisonOp into a template if you wish. ------------------------------------------------ #include #include #include class ComparisonOp : public std::binary_function { public: result_type operator()( const first_argument_type &a, const second_argument_type &b ) const { return (a < b); } }; int main() { std::vector::iterator p = std::max_element(m.begin(), m.end(), ComparisonOp()); } ---------------------------------------------------------- This uses boost lambda. ---------------------------------------------------------- #include #include #include // this works if operator< is defined for your type (int in your case) using namespace boost::lambda; int main() { std::vector::iterator p = std::max_element( m.begin(), m.end(), (_1 < _2) ); } ------------------------------------------------------------- Oops I fogot to put the abs function/fabs if float/double in there but you get, the idea :) HTH Mar 14 '07 #3

 P: n/a On Wed, 14 Mar 2007 00:56:16 GMT in comp.lang.c++, JDT Hi,Can someone provide me an example that uses std::max_element()(probablly the predicate version) to return the max "absolute" integerin a vector? Your help is much appreciated.Tonyps.// return the max integer in a vectorstd::vector::iterator p = std::max_element(m.begin(), m.end()); I don't think I understand your question. Are you asking how to get from the iterator p to the vector element value? Mar 14 '07 #4

 P: n/a Hi Piyo, I totally understand your solution and appreciate your help. I wonder if there is a way to put different, existing STL algorithms together to achieve the same function as your class ComparisonOp? Any further help is appreciated. Tony Piyo wrote: Piyo wrote: >JDT wrote: >>Hi,Can someone provide me an example that uses std::max_element()(probablly the predicate version) to return the max "absolute"integer in a vector? Your help is much appreciated.Tonyps.// return the max integer in a vectorstd::vector::iterator p = std::max_element(m.begin(), m.end()); This is the traditional way. You can also attemp to make ComparisonOpinto a template if you wish.------------------------------------------------#include #include #include class ComparisonOp : public std::binary_function{public: result_type operator()( const first_argument_type &a, const second_argument_type &b ) const { return (a < b); }};intmain(){ std::vector::iterator p = std::max_element(m.begin(), m.end(), ComparisonOp());}----------------------------------------------------------This uses boost lambda.----------------------------------------------------------#include #include #include // this works if operator< is defined for your type (int in your case)using namespace boost::lambda;intmain(){ std::vector::iterator p = std::max_element( m.begin(), m.end(), (_1 < _2) );}------------------------------------------------------------- Oops I fogot to put the abs function/fabs if float/double in there but you get, the idea :) HTH Mar 14 '07 #5

 P: n/a David Harmon wrote: Hi David, That's not what I meant. What I need is a predicate that compares the absolute values of the vector elements. It's best that the predicate is composed of some existing STL algorithms, predicates and/or containers. Thanks. Tony On Wed, 14 Mar 2007 00:56:16 GMT in comp.lang.c++, JDT >Hi,Can someone provide me an example that uses std::max_element()(probablly the predicate version) to return the max "absolute" integerin a vector? Your help is much appreciated.Tonyps.// return the max integer in a vectorstd::vector::iterator p = std::max_element(m.begin(), m.end()); I don't think I understand your question. Are you asking how to get from the iterator p to the vector element value? Mar 14 '07 #6

 P: n/a JDT wrote: Hi Piyo, I totally understand your solution and appreciate your help. I wonder if there is a way to put different, existing STL algorithms together to achieve the same function as your class ComparisonOp? Any further help is appreciated. Tony OH, Ok. Then get the min and max of the vector and then negate the min and max if any are negative and then get the max of those. Is that what you are talking about? Each step can be done with existing stl algorithms. Or were you thinking of something else? HTH Mar 14 '07 #7

 P: n/a JDT wrote: Hi Piyo, I totally understand your solution and appreciate your help. I wonder if there is a way to put different, existing STL algorithms together to achieve the same function as your class ComparisonOp? Any further help is appreciated. Tony Piyo wrote: >Piyo wrote: >>JDT wrote:Hi,Can someone provide me an example that uses std::max_element()(probablly the predicate version) to return the max "absolute"integer in a vector? Your help is much appreciated.Tonyps.// return the max integer in a vectorstd::vector::iterator p = std::max_element(m.begin(), m.end()); This is the traditional way. You can also attemp to make ComparisonOpinto a template if you wish.------------------------------------------------#include #include #include class ComparisonOp : public std::binary_function{public: result_type operator()( const first_argument_type &a, const second_argument_type &b ) const { return (a < b); }};intmain(){ std::vector::iterator p = std::max_element(m.begin(), m.end(), ComparisonOp());}----------------------------------------------------------This uses boost lambda.----------------------------------------------------------#include #include #include // this works if operator< is defined for your type (int in your case)using namespace boost::lambda;intmain(){ std::vector::iterator p = std::max_element( m.begin(), m.end(), (_1 < _2) );}------------------------------------------------------------- Oops I fogot to put the abs function/fabs if float/doublein there but you get, the idea :)HTH or here is another idea: from std::max from std::less then std::max( -a, a ) gives the absolute value then less( max(-a,a), max(-b,b ) ) defines the less than operator of the absoulte value. Is this what you wanted? Mar 14 '07 #8

 P: n/a Hi Piyo, It's exactly what I meant. I am going to try your less( max(-a,a), max(-b,b ) ) solution. Hope it will work. Thanks so much. Tony Piyo wrote: JDT wrote: >Hi Piyo,I totally understand your solution and appreciate your help. I wonderif there is a way to put different, existing STL algorithms togetherto achieve the same function as your class ComparisonOp? Any furtherhelp is appreciated.TonyPiyo wrote: >>Piyo wrote:JDT wrote:Hi,>Can someone provide me an example that uses std::max_element()(probablly the predicate version) to return the max "absolute"integer in a vector? Your help is much appreciated.>Tony>ps.>// return the max integer in a vectorstd::vector::iterator p = std::max_element(m.begin(), m.end()); This is the traditional way. You can also attemp to make ComparisonOpinto a template if you wish.------------------------------------------------#include #include #include class ComparisonOp : public std::binary_function{public: result_type operator()( const first_argument_type &a, const second_argument_type &b ) const { return (a < b); }};intmain(){ std::vector::iterator p = std::max_element(m.begin(), m.end(), ComparisonOp());}----------------------------------------------------------This uses boost lambda.----------------------------------------------------------#include #include #include // this works if operator< is defined for your type (int in your case)using namespace boost::lambda;intmain(){ std::vector::iterator p = std::max_element( m.begin(), m.end(), (_1 < _2) );}------------------------------------------------------------- Oops I fogot to put the abs function/fabs if float/doublein there but you get, the idea :)HTH or here is another idea: from std::max from std::less then std::max( -a, a ) gives the absolute value then less( max(-a,a), max(-b,b ) ) defines the less than operator of the absoulte value. Is this what you wanted? Mar 14 '07 #9

 P: n/a Hi Piyo, I tried your idea but didn't make it work. I am not familiar with STL syntax. Could you kindly give me some hint? Thanks for your further help. Tony Piyo wrote: JDT wrote: >Hi Piyo,I totally understand your solution and appreciate your help. I wonderif there is a way to put different, existing STL algorithms togetherto achieve the same function as your class ComparisonOp? Any furtherhelp is appreciated.TonyPiyo wrote: >>Piyo wrote:JDT wrote:Hi,>Can someone provide me an example that uses std::max_element()(probablly the predicate version) to return the max "absolute"integer in a vector? Your help is much appreciated.>Tony>ps.>// return the max integer in a vectorstd::vector::iterator p = std::max_element(m.begin(), m.end()); This is the traditional way. You can also attemp to make ComparisonOpinto a template if you wish.------------------------------------------------#include #include #include class ComparisonOp : public std::binary_function{public: result_type operator()( const first_argument_type &a, const second_argument_type &b ) const { return (a < b); }};intmain(){ std::vector::iterator p = std::max_element(m.begin(), m.end(), ComparisonOp());}----------------------------------------------------------This uses boost lambda.----------------------------------------------------------#include #include #include // this works if operator< is defined for your type (int in your case)using namespace boost::lambda;intmain(){ std::vector::iterator p = std::max_element( m.begin(), m.end(), (_1 < _2) );}------------------------------------------------------------- Oops I fogot to put the abs function/fabs if float/doublein there but you get, the idea :)HTH or here is another idea: from std::max from std::less then std::max( -a, a ) gives the absolute value then less( max(-a,a), max(-b,b ) ) defines the less than operator of the absoulte value. Is this what you wanted? Mar 14 '07 #10

 P: n/a JDT wrote: Hi Piyo, I tried your idea but didn't make it work. I am not familiar with STL syntax. Could you kindly give me some hint? Thanks for your further help. Tony >>from std::maxfrom std::lessthenstd::max( -a, a ) gives the absolute valuethen less( max(-a,a), max(-b,b ) ) defines the less thanoperator of the absoulte value.Is this what you wanted? I would just stick it in a function #include #include bool foo( int a, int b ) { return std::less( std::max(-a,a), std::max(-b,b) ); } // then int main() { std::vector

### This discussion thread is closed

Replies have been disabled for this discussion. 