By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

template <class C> void list::sort( C fn ) question

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Ron Natalie wrote:

"Math Preregistration System" <pr******@beta.math.uwaterloo.ca> wrote
in message news:bd**********@tabloid.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.
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());
Jul 19 '05 #2

P: n/a
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.
{
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


Jul 19 '05 #3

P: n/a

"Rolf Magnus" <ra******@t-online.de> wrote in message news:bd************@news.t-online.com...
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.
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.