Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Setting predicate for std::list::sort().

Reply
Thread Tools

Setting predicate for std::list::sort().

 
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      03-18-2008
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-18-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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:air<double,MyType> const &p1,
std:air<double,MyType> const &p2)
{
return p1.first < p2.first;
}

...
std::list<std:air ...> 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


 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      03-18-2008
(E-Mail Removed) wrote:
> 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:air<double, MyType> pr;
bool operator()(const pr& lhs, const pr& rhs) const
{
// do the compare and return accordingly
}
}

std::list<std:air<double, MyType> > l;

l.sort(comp());
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      03-18-2008
(E-Mail Removed) wrote:
> 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:air<double, MyType> >(
const std:air<double, MyType>& lhs,
const std:air<double, MyType>& rhs)
{
return lhs.d < rhs.d;
}
 
Reply With Quote
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      03-18-2008
On Mar 18, 5:55 pm, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> 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.


Thanks Victor and red floyd.

The problem was that I don't use std::list::sort() much, jumped to a
conclusion about a different compiler error; and then, unfortunately,
also missed the mention of the overloaded sort() in the documentation
I looked at. So I screwed it up and there was no problem. Thanks for
answering.

Jason
 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XPATH Predicate Syntax question Sabba XML 0 05-09-2006 10:48 AM
get position after spec. predicate awebguynow XML 1 01-27-2006 08:42 AM
Predicate by attribute? Jacques XML 2 06-24-2004 11:45 AM
std::stable_sort with predicate woes William Payne C++ 8 04-21-2004 12:37 PM
Help with template find_if predicate mikets C++ 18 01-07-2004 02:36 PM



Advertisments