473,943 Members | 11,143 Online

# get the max "absolute" integer in a vector

Hi,

Can someone provide me an example that uses std::max_elemen t()
(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<int m;
....
std::vector<int >::iterator p = std::max_elemen t(m.begin(), m.end());
Mar 14 '07 #1
10 8448
JDT wrote:
Hi,

Can someone provide me an example that uses std::max_elemen t()
(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<int m;
...
std::vector<int >::iterator p = std::max_elemen t(m.begin(), m.end());
This is the traditional way. You can also attemp to make ComparisonOp
into a template if you wish.

------------------------------------------------
#include <vector>
#include <algorithm>
#include <functional>

class ComparisonOp : public std::binary_fun ction<bool, int, int>
{
public:
result_type operator()( const first_argument_ type &a,
const second_argument _type &b ) const
{
return (a < b);
}
};

int
main()
{
std::vector<int m;
// fill m with elements
std::vector<int >::iterator p = std::max_elemen t(m.begin(), m.end(),
ComparisonOp()) ;
}
----------------------------------------------------------

This uses boost lambda.
----------------------------------------------------------
#include <vector>
#include <algorithm>
#include <boost/lambda/lambda.hpp>

// this works if operator< is defined for your type (int in your case)

using namespace boost::lambda;

int
main()
{
std::vector<int m;
// fill m with elements
std::vector<int >::iterator p =
std::max_elemen t( m.begin(), m.end(), (_1 < _2) );
}

-------------------------------------------------------------
Mar 14 '07 #2
Piyo wrote:
JDT wrote:
>Hi,

Can someone provide me an example that uses std::max_elemen t()
(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<in tm;
...
std::vector<in t>::iterator p = std::max_elemen t(m.begin(), m.end());

This is the traditional way. You can also attemp to make ComparisonOp
into a template if you wish.

------------------------------------------------
#include <vector>
#include <algorithm>
#include <functional>

class ComparisonOp : public std::binary_fun ction<bool, int, int>
{
public:
result_type operator()( const first_argument_ type &a,
const second_argument _type &b ) const
{
return (a < b);
}
};

int
main()
{
std::vector<int m;
// fill m with elements
std::vector<int >::iterator p = std::max_elemen t(m.begin(), m.end(),
ComparisonOp()) ;
}
----------------------------------------------------------

This uses boost lambda.
----------------------------------------------------------
#include <vector>
#include <algorithm>
#include <boost/lambda/lambda.hpp>

// this works if operator< is defined for your type (int in your case)

using namespace boost::lambda;

int
main()
{
std::vector<int m;
// fill m with elements
std::vector<int >::iterator p =
std::max_elemen t( 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
On Wed, 14 Mar 2007 00:56:16 GMT in comp.lang.c++, JDT
<jd*******@yaho o.comwrote,
>Hi,

Can someone provide me an example that uses std::max_elemen t()
(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<in tm;
...
std::vector<in t>::iterator p = std::max_elemen t(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
Hi Piyo,

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_elemen t()
(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<i ntm;
...
std::vector<i nt>::iterator p = std::max_elemen t(m.begin(), m.end());

This is the traditional way. You can also attemp to make ComparisonOp
into a template if you wish.

------------------------------------------------
#include <vector>
#include <algorithm>
#include <functional>

class ComparisonOp : public std::binary_fun ction<bool, int, int>
{
public:
result_type operator()( const first_argument_ type &a,
const second_argument _type &b ) const
{
return (a < b);
}
};

int
main()
{
std::vector<int m;
// fill m with elements
std::vector<int >::iterator p = std::max_elemen t(m.begin(), m.end(),
ComparisonOp()) ;
}
----------------------------------------------------------

This uses boost lambda.
----------------------------------------------------------
#include <vector>
#include <algorithm>
#include <boost/lambda/lambda.hpp>

// this works if operator< is defined for your type (int in your case)

using namespace boost::lambda;

int
main()
{
std::vector<int m;
// fill m with elements
std::vector<int >::iterator p =
std::max_elemen t( 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
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
<jd*******@yaho o.comwrote,
>>Hi,

Can someone provide me an example that uses std::max_elemen t()
(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<i ntm;
...
std::vector<i nt>::iterator p = std::max_elemen t(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
JDT wrote:
Hi Piyo,

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
JDT wrote:
Hi Piyo,

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_elemen t()
(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< intm;
...
std::vector< int>::iterator p = std::max_elemen t(m.begin(), m.end());
This is the traditional way. You can also attemp to make ComparisonOp
into a template if you wish.

------------------------------------------------
#include <vector>
#include <algorithm>
#include <functional>

class ComparisonOp : public std::binary_fun ction<bool, int, int>
{
public:
result_type operator()( const first_argument_ type &a,
const second_argument _type &b ) const
{
return (a < b);
}
};

int
main()
{
std::vector<int m;
// fill m with elements
std::vector<int >::iterator p = std::max_elemen t(m.begin(), m.end(),
ComparisonOp()) ;
}
----------------------------------------------------------

This uses boost lambda.
----------------------------------------------------------
#include <vector>
#include <algorithm>
#include <boost/lambda/lambda.hpp>

// this works if operator< is defined for your type (int in your case)

using namespace boost::lambda;

int
main()
{
std::vector<int m;
// fill m with elements
std::vector<int >::iterator p =
std::max_elemen t( 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
or here is another idea:

from <algorithm>
std::max
from <functional>
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
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,

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_elemen t()
(probabll y 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 <intm;
...
std::vector <int>::iterat or p = std::max_elemen t(m.begin(), m.end());

This is the traditional way. You can also attemp to make ComparisonOp
into a template if you wish.

------------------------------------------------
#include <vector>
#include <algorithm>
#include <functional>

class ComparisonOp : public std::binary_fun ction<bool, int, int>
{
public:
result_type operator()( const first_argument_ type &a,
const second_argument _type &b ) const
{
return (a < b);
}
};

int
main()
{
std::vector<int m;
// fill m with elements
std::vector<int >::iterator p = std::max_elemen t(m.begin(), m.end(),
ComparisonOp()) ;
}
----------------------------------------------------------

This uses boost lambda.
----------------------------------------------------------
#include <vector>
#include <algorithm>
#include <boost/lambda/lambda.hpp>

// this works if operator< is defined for your type (int in your case)

using namespace boost::lambda;

int
main()
{
std::vector<int m;
// fill m with elements
std::vector<int >::iterator p =
std::max_elemen t( 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

or here is another idea:

from <algorithm>
std::max
from <functional>
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
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,

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_elemen t()
(probabll y 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 <intm;
...
std::vector <int>::iterat or p = std::max_elemen t(m.begin(), m.end());

This is the traditional way. You can also attemp to make ComparisonOp
into a template if you wish.

------------------------------------------------
#include <vector>
#include <algorithm>
#include <functional>

class ComparisonOp : public std::binary_fun ction<bool, int, int>
{
public:
result_type operator()( const first_argument_ type &a,
const second_argument _type &b ) const
{
return (a < b);
}
};

int
main()
{
std::vector<int m;
// fill m with elements
std::vector<int >::iterator p = std::max_elemen t(m.begin(), m.end(),
ComparisonOp()) ;
}
----------------------------------------------------------

This uses boost lambda.
----------------------------------------------------------
#include <vector>
#include <algorithm>
#include <boost/lambda/lambda.hpp>

// this works if operator< is defined for your type (int in your case)

using namespace boost::lambda;

int
main()
{
std::vector<int m;
// fill m with elements
std::vector<int >::iterator p =
std::max_elemen t( 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

or here is another idea:

from <algorithm>
std::max
from <functional>
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

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 43 5175 by: steve | last post by: I am quite frustrated with php’s include, as I have spent a ton of time on it already... anyone can tell me why it was designed like this (or something I don’t get)? The path in include is relative NOT to the immediate script that is including it, but is relative to the top-level calling script. In practice, this means that you have to constantly worry and adjust paths in includes, based on the startup scripts that call these... 3 46976 by: ¤ Alias | last post by: I have a function named getID3info (lvwDiscInfo.SelectedItem). What is the difference between getID3info (lvwDiscInfo.SelectedItem) and Call getID3info(lvwDiscInfo.SelectedItem) ? 86 4073 by: Randy Yates | last post by: In Harbison and Steele's text (fourth edition, p.111) it is stated, The C language does not specify the range of integers that the integral types will represent, except ot say that type int may not be smaller than short and long may not be smaller than int. They go on to say, Many implementations represent characters in 8 bits, type short in 14 2121 by: TTroy | last post by: Hello, can anyone explain why the following function will not work for INT_MIN: /* itoa: convert n to characters in s */ void itoa(int n, char s) { int i, sign; if((sign = n) < 0) /* record sign */ 2 7729 by: bay_dar | last post by: Hi, I have an internal ASP.NET application that I'm are using to send e-mails out based on a single milepost or milepost range entered. I'm trying to do two things when a user clicks on the submit button to submit a form that contains one or two Mileposts: 1) If a Milepost range larger than 5 miles is entered, I would like to pop up a confirmation box to confirm the range. 8 2016 by: vitay | last post by: Hi Centered links are hidden by the footer in resolution 800x600 because divs have position absolute and I don't know how to prevent it. For IE I can change position by css "expresion" but Firefox don't support it. My page: http://exterior.pl/bumaga/ 4 29408 by: john | last post by: Hi to All, I am new to html authoring, so sorry if my terminology is not correct or exact. I would like to position a footer div to the bottom of the browser window. As I research in the web shows this could be done a containing element with "min-height: 100%;" and an absolute positioned element (div) inside, aligned to bottom ("position: absolute; bottom:0px") 7 2410 by: raylaur | last post by: I'm using a javascript "slide" function to move a