By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 999 Online
Bytes IT Community
+ Ask a Question
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
JDT
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<intm;
....
std::vector<int>::iterator p = std::max_element(m.begin(), m.end());
Mar 14 '07 #1
Share this Question
Share on Google+
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<intm;
...
std::vector<int>::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 <vector>
#include <algorithm>
#include <functional>

class ComparisonOp : public std::binary_function<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<intm;
// fill m with elements
std::vector<int>::iterator p = std::max_element(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<intm;
// fill m with elements
std::vector<int>::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" 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_element(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_function<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<intm;
// fill m with elements
std::vector<int>::iterator p = std::max_element(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<intm;
// fill m with elements
std::vector<int>::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
<jd*******@yahoo.comwrote,
>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<intm;
...
std::vector<int>::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
JDT
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.

Tony

ps.

// return the max integer in a vector
std::vector<intm;
...
std::vector<int>::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 <vector>
#include <algorithm>
#include <functional>

class ComparisonOp : public std::binary_function<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<intm;
// fill m with elements
std::vector<int>::iterator p = std::max_element(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<intm;
// fill m with elements
std::vector<int>::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
JDT
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*******@yahoo.comwrote,
>>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<intm;
...
std::vector<int>::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.

Tony

ps.

// return the max integer in a vector
std::vector<intm;
...
std::vector<int>::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 <vector>
#include <algorithm>
#include <functional>

class ComparisonOp : public std::binary_function<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<intm;
// fill m with elements
std::vector<int>::iterator p = std::max_element(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<intm;
// fill m with elements
std::vector<int>::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
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

P: n/a
JDT
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 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.
>
Tony
>
ps.
>
// return the max integer in a vector
std::vector<intm;
...
std::vector<int>::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 <vector>
#include <algorithm>
#include <functional>

class ComparisonOp : public std::binary_function<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<intm;
// fill m with elements
std::vector<int>::iterator p = std::max_element(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<intm;
// fill m with elements
std::vector<int>::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


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

P: n/a
JDT
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 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.
>
Tony
>
ps.
>
// return the max integer in a vector
std::vector<intm;
...
std::vector<int>::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 <vector>
#include <algorithm>
#include <functional>

class ComparisonOp : public std::binary_function<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<intm;
// fill m with elements
std::vector<int>::iterator p = std::max_element(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<intm;
// fill m with elements
std::vector<int>::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


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

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 <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?
I would just stick it in a function
#include <algorithm>
#include <functional>
bool foo( int a, int b )
{
return std::less( std::max(-a,a), std::max(-b,b) );
}

// then
int
main()
{
std::vector<intm;
std::sort( m.begin(), m.end(), &foo );
}
Mar 14 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.