std::list & sorting

V

vertigo

Hello
I have:
std::list<ObjPtr *> mylist;
i wanted to sort that pointers using my own sorting function:
mylist.sort(MySortingFunction);

I can't create that function, i receive many errors :( could anybody
show how sould it look like ?

Thanx
Michal
 
I

Ivan Vecerina

vertigo said:
I have:
std::list<ObjPtr *> mylist;
i wanted to sort that pointers using my own sorting function:
mylist.sort(MySortingFunction);

I can't create that function, i receive many errors :( could anybody show
how sould it look like ?

As a function:
bool MySortingFunction(ObjPtr* a, ObjPtr* b)
{ return a->field < b->filed; }

-> mylist.sort( & MySortingFunction );


Note that using a function object might generate faster code
(on typical platforms) because it enables inlining of the
predicate.
You could start from something like:
struct MySorter {
bool operator()(ObjPtr* a, ObjPtr* b) const
{ return a->field < b->filed; }
};

-> mylist.sort( MySorter() );


I hope this helps,
Ivan
 

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,660
Latest member
vidotip479

Latest Threads

Top