get the max "absolute" integer in a vector

J

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<int> m;
....
std::vector<int>::iterator p = std::max_element(m.begin(), m.end());
 
P

Piyo

JDT said:
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<int> m;
...
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<int> m;
// 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<int> m;
// fill m with elements
std::vector<int>::iterator p =
std::max_element( m.begin(), m.end(), (_1 < _2) );
}

-------------------------------------------------------------
 
P

Piyo

Piyo said:
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<int> m;
// 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<int> m;
// 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
 
D

David Harmon

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" 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_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?
 
J

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
 
J

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
 
P

Piyo

JDT said:
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
 
P

Piyo

JDT said:
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

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?
 
J

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
 
J

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
 
P

Piyo

JDT said:
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

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<int> m;
std::sort( m.begin(), m.end(), &foo );
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top