Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 18th, 2008, 11:05 PM
jason.cipriani@gmail.com
Guest
 
Posts: n/a
Default Setting predicate for std::list::sort().

How can I use my own custom comparison function with
std::list::sort()? The only call to sort() I see does not take a
predicate argument.

Specifically, I have:

list<pair<double,MyType ...;

And I want to sort only on the value of the first member in that pair.
My solution right now is to not use a pair; doing something like this
instead:

struct A {
double d;
MyType t;
bool operator < (const A &a) const {
return d < a.d;
}
};

list<A...;

But I am wondering how to do it in general.

Thanks,
Jason
  #2  
Old March 18th, 2008, 11:05 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Setting predicate for std::list::sort().

jason.cipriani@gmail.com wrote:
Quote:
How can I use my own custom comparison function with
std::list::sort()? The only call to sort() I see does not take a
predicate argument.
>
Specifically, I have:
>
list<pair<double,MyType ...;
>
And I want to sort only on the value of the first member in that pair.
My solution right now is to not use a pair; doing something like this
instead:
>
struct A {
double d;
MyType t;
bool operator < (const A &a) const {
return d < a.d;
}
};
>
list<A...;
>
But I am wondering how to do it in general.
What seems to be the problem?

bool firstIsLess(std::pair<double,MyTypeconst &p1,
std::pair<double,MyTypeconst &p2)
{
return p1.first < p2.first;
}

...
std::list<std::pair ...myList;

myList.sort(firstIsLess);

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  #3  
Old March 18th, 2008, 11:05 PM
red floyd
Guest
 
Posts: n/a
Default Re: Setting predicate for std::list::sort().

jason.cipriani@gmail.com wrote:
Quote:
How can I use my own custom comparison function with
std::list::sort()? The only call to sort() I see does not take a
predicate argument.
>
Specifically, I have:
>
list<pair<double,MyType ...;
>
And I want to sort only on the value of the first member in that pair.
My solution right now is to not use a pair; doing something like this
instead:
>
struct A {
double d;
MyType t;
bool operator < (const A &a) const {
return d < a.d;
}
};
>
list<A...;
>
But I am wondering how to do it in general.
>
std::list::sort is overloaded. One takes no parameters, using std::less
as the predicate, the other takes a predicate.

e.g.

struct comp {
typedef std::pair<double, MyTypepr;
bool operator()(const pr& lhs, const pr& rhs) const
{
// do the compare and return accordingly
}
}

std::list<std::pair<double, MyType l;

l.sort(comp());
  #4  
Old March 18th, 2008, 11:15 PM
red floyd
Guest
 
Posts: n/a
Default Re: Setting predicate for std::list::sort().

jason.cipriani@gmail.com wrote:
Quote:
How can I use my own custom comparison function with
std::list::sort()? The only call to sort() I see does not take a
predicate argument.
>
Specifically, I have:
>
list<pair<double,MyType ...;
>
And I want to sort only on the value of the first member in that pair.
My solution right now is to not use a pair; doing something like this
instead:
>
struct A {
double d;
MyType t;
bool operator < (const A &a) const {
return d < a.d;
}
};
>
list<A...;
>
But I am wondering how to do it in general.
>
Plus, you are allowed to overload std::less for your types -- one
of the rare cases where you are allowed to mess around in std::.

e.g:

template<>
bool std::less<std::pair<double, MyType(
const std::pair<double, MyType>& lhs,
const std::pair<double, MyType>& rhs)
{
return lhs.d < rhs.d;
}
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles