473,387 Members | 1,529 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.

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
Mar 18 '08 #1
3 3349
ja************@gmail.com 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::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
Mar 18 '08 #2
ja************@gmail.com 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::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());
Mar 18 '08 #3
ja************@gmail.com 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::pair<double, MyType(
const std::pair<double, MyType>& lhs,
const std::pair<double, MyType>& rhs)
{
return lhs.d < rhs.d;
}
Mar 18 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Math Preregistration System | last post by:
I'm using a std::list as a container for some pointers to objects, for example list< C* > lst; I would like to sort them using two different criteria, say first by C.first and then by...
6
by: Tim Partridge | last post by:
I have a std list of student objects and I would like to sort them by providing a comparison function. I have gotten it to work if my function is global, but I would like to encapsulate it in a...
14
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for...
11
by: velthuijsen | last post by:
I tried taking a list and pass it through std::sort like the following: sort(Unsorted.begin(), Unsorted.end()); I got an error back stating that the list iterator doesn't have a binary...
3
by: Jef Driesen | last post by:
The number of items in my std::list is changed after sorting (using std::list member function sort). I'm using MSVC6 and the actual (pseudo) code is posted below. The output from size() is...
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
4
by: Tim Slattery | last post by:
It would be convenient for my app to store the stuff I'm generating in a std::list. I'd like to remember the location of a particular place in the list - sort of like sticking my finger into it -...
15
by: desktop | last post by:
If I have a sorted std::list with 1.000.000 elements it takes 1.000.000 operations to find element with value = 1.000.000 (need to iterator through the whole list). In comparison, if I have a...
19
by: Juha Nieminen | last post by:
If I'm not completely mistaken, the only reason why std::list::size() may be (and usually is) a linear-time operation is because they want std::list::splice() to be a constant-time operation, and...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.