473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

get the max "absolute" integer in a vector

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
10 8336
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
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...
3
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
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...
14
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) /*...
2
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...
8
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...
4
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...
7
by: raylaur | last post by:
I'm using a javascript "slide" function to move a <div> layer in 10 pixel increments from one location on a page to another. The layer contains a GIF image. It's basically a side panel that flies out...
2
by: Richard Maher | last post by:
Hi, Can someone please tell me the strategy(ies) used by Java (the Security Manager or whatever) to determine if a given IP address conforms to the definition of the codebase from which an...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.