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

J

jason.cipriani

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
 
V

Victor Bazarov

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,MyType> const &p1,
std::pair<double,MyType> const &p2)
{
return p1.first < p2.first;
}

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

myList.sort(firstIsLess);

V
 
R

red floyd

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, MyType> pr;
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());
 
R

red floyd

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;
}
 
J

jason.cipriani

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
 

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,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top