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

list::sort with pointers

How do I define the use of list::sort() when the list holds pointers? For
example,

#include <list>

class foo {
public:
foo(int i): i(i) {};
~foo();

private:
int i;
};

void main() {
foo *f = new foo(3);
foo *g = new foo(2);

list< foo* > l;
l.push_back(f);
l.push_back(g);

l.sort(); // I want this to sort based on foo::i
}
Thanks,
Tim Partridge

Jul 19 '05 #1
2 6314
On Wed, 13 Aug 2003 12:35:27 -0400
Tim Partridge <tj******@lassar.math.uwaterloo.ca> wrote:
How do I define the use of list::sort() when the list holds pointers?
For example,

#include <list>

class foo {
public:
foo(int i): i(i) {};
~foo();

private:
int i;
};

void main() {
foo *f = new foo(3);
foo *g = new foo(2);

list< foo* > l;
l.push_back(f);
l.push_back(g);

l.sort(); // I want this to sort based on foo::i
}


You can't call sort on a list with pointers. You have to specify a
sorting function: l.sort(someFunction);

br
socketd
Jul 19 '05 #2

"Tim Partridge" <tj******@lassar.math.uwaterloo.ca> wrote in message
news:Pi*************************************@lassa r.math.uwaterloo.ca...
How do I define the use of list::sort() when the list holds pointers? For
example,

#include <list>

class foo {
public:
foo(int i): i(i) {};
~foo();

private:
int i;
};

void main() {
foo *f = new foo(3);
foo *g = new foo(2);

list< foo* > l;
l.push_back(f);
l.push_back(g);

l.sort(); // I want this to sort based on foo::i
}
Thanks,
Tim Partridge


bool sort_func(foo* lhs, foo* rhs)
{
return lhs->i < rhs->i;
}

l.sort(sort_func);

Aside from the fact that i is private, that should work, but untested code.

john
Jul 19 '05 #3

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

Similar topics

5
by: Amy G | last post by:
I have a list of numbers... actully I have two lists, List 1 is a list of number strings and List2 is one of numbers. List 1 example: List1 = List 2 example: List2 = When I try either:
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...
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
11
by: ankyhe | last post by:
L = L=L.sort() L will refer to None, why L.sort() don't return the L? I want to ask why the designer of Python do so?
0
by: Amar | last post by:
Hi, I have a generic list with a some objects of a particular class in it. I have implemented a IComparer for the the class and pass it to the List. The list.sort method works fine when the value...
14
by: gjin | last post by:
Hi there, I met a problem, which I could not solve. I used it as the following: list<longL for (long i=1;i<=479250; i++) L.push_back(i);
3
by: jason.cipriani | last post by:
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 ...; ...
1
by: Steven Clark | last post by:
If I have a list of items of mixed type, can I put something into it such that after a list.sort(), is guaranteed to be at the end of the list? Looking at...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.