 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Math Preregistration Syst Guest
|
Posted: Fri Jun 27, 2003 3:27 pm Post subject: template <class C> void list::sort( C fn ) question |
|
|
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 C.second. Is it possible to use template <class C>
void list::sort( C fn ) and define two different fns, and use them
something like
lst.sort< C >( sortOnFirst() );
lst.sort< C >( sortOnSecond() ):
or am I not on the right track? I don't understand exactly how this
works. Alternatively, I can use a vector and std::sort() if you can
convince me it would be a better idea, but the main point is that I need
to sort according to two different criteria.
Thanks for you input!
Tim Partridge
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Fri Jun 27, 2003 4:40 pm Post subject: Re: template <class C> void list::sort( C fn ) question |
|
|
Ron Natalie wrote:
| Quote: |
"Math Preregistration System" <preregd1 (AT) beta (DOT) math.uwaterloo.ca> wrote
in message news:bdhno8$pon$1 (AT) tabloid (DOT) uwaterloo.ca...
lst.sort< C >( sortOnFirst() );
lst.sort< C >( sortOnSecond() ):
or am I not on the right track? I don't understand exactly how this
works.
You need to define sortOnFirst, you don't want to put the parens
there.
|
You do if you make them function objects.
| Quote: | You haven't give any clue as to the nature of C, but your functions
should look something like:
bool sortOnFirst(const C& left, const C& right) {
// return true if left is logically less than right in this
sorting order
// ...
}
lst.sort<C>(sortOnFirst)
|
Or:
struct sortOnFirst : public std::binary_function<C, C, bool>
{
bool operator()(const C& left, const C& right) const
{
// return true if left is logically less than right in this
sorting order
// ...
}
};
lst.sort(sortOnFirst());
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Fri Jun 27, 2003 4:42 pm Post subject: Re: template <class C> void list::sort( C fn ) question |
|
|
Klaus Eichner wrote:
| Quote: | inline bool sortOnFirst_then_Second(C* a, C* b)
|
Note that the inline keyword here doesn't provide any benefit, since the
function will be called through a pointer, which excludes the
possibility to expand the function inline. If you want that, you need a
function object.
| Quote: | {
if (sortOnFirst(a,b)) return true;
else if (sortOnFirst(b,a)) return false;
else {// ...here 'a' and 'b' are considered
// to be "equal" according to
// sortOnFirst, therefore we have
// to consult sortOnSecond...
return sortOnSecond(a,b);
}
...and executing std::list::sort only once ?
lst.sort(sortOnFirst_then_Second);
Thanks for you input!
Tim Partridge
|
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Fri Jun 27, 2003 5:45 pm Post subject: Re: template <class C> void list::sort( C fn ) question |
|
|
"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote
| Quote: | Klaus Eichner wrote:
inline bool sortOnFirst_then_Second(C* a, C* b)
Note that the inline keyword here doesn't provide any benefit, since the
function will be called through a pointer, which excludes the
possibility to expand the function inline. If you want that, you need a
function object.
|
That and the objects it wants to pass to it are C's not C*'s.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|