473,395 Members | 1,452 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,395 software developers and data experts.

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

Jul 19 '05 #1
3 6089
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michele | last post by:
Slowly embracing C++ and as such I've got C code (see snippet below) I'd like to essentially port to C++. The idea is to 1. Use a TEMPLATE class to handle the 'double' buffering scheme...
4
by: Joe User | last post by:
How would you count the number of columns with a value not equal to 0 for each row in a table? Thanks! Joe
5
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: ...
10
by: Paul Schneider | last post by:
I want to sort a class derived from std::vector with STL sort.: template<typename T, typename fitParaType, typename fitResType> class Manipulator{ // shouldn't I now be able to access private...
1
by: mast2as | last post by:
Hi there, Here is the conceptual problem i try to find an elegant solution to. I have a template class that I use to save data from a file. The data can be integer, float, double. This is a...
4
by: DancnDude | last post by:
I have a class that needs to have several different kinds of sorting routines on an ArrayList that it needs to conditionally do based upon the data. I have successfully created a class that...
2
by: Ivan Liu | last post by:
Hi, I have a question as in the title. I have two choices. One is the template function template <class T> Add(T a, T b){ return a+b; } and the template class or functor
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
4
by: Anu | last post by:
Hi, We have a class that has its own overloaded operator new and whose prototype seems to correspond to the standard placement new :- class AppClass { public: operator new (size_t size,...
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: 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...
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?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.