Connecting Tech Pros Worldwide Help | Site Map

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

jason.cipriani@gmail.com
Guest
 
Posts: n/a
#1: Mar 18 '08
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
Victor Bazarov
Guest
 
Posts: n/a
#2: Mar 18 '08

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


red floyd
Guest
 
Posts: n/a
#3: Mar 18 '08

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());
red floyd
Guest
 
Posts: n/a
#4: Mar 18 '08

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;
}
Closed Thread