Re: Sorting a map<T*>
On May 8, 2:01 am, Markus Schoder <a3vr6dsg-use...@yahoo.de> wrote:
On Mon, 07 May 2007 19:45:00 -0400, Pete Becker wrote:
Markus Schoder wrote:
On Mon, 07 May 2007 16:11:52 -0700, Michael wrote:
On May 7, 3:01 pm, "barcaroller" <barcarol...@music.net> wrote:
I have a set<T*> that stores pointers to objects. How can I tell
set<T*> to use the objects' operator<() and not the value of the
pointers for sorting?
There's a way to create a set with two parameters: set<MyType,
key_comp>, where
key_comp is a key comparison function you define. For example,
set<int, less<int> >.
So then you'd just need to provide the appropriate comparison function
that uses
the objects' operator<(), suitably wrapped.
Actually you need a class with an appropriate operator(). A plain
function won't do here since you cannot pass it as a template
parameter.
Sure you can. Try it.
No you cannot. I guess you are confusing this with algorithms like
std::lower_bound where you can specify a predicate as a _function_
parameter. As a template parameter for std::set or std::map this does not
work. Think about it -- it needs to be a type.
Now I've seen everything. Someone trying to correct Pete with
regards to the standard library.
Whether you're aware of it or not, pointer to a function *is* a
type, and there's no problem declaring an std::set to take a
pointer to a function, e.g.:
bool
cmp( T const* p1, T const* p2 )
{
return *p1 < *p2 ;
}
std::set< T*, bool (*)( T const*, T const* ) > mySet( cmp ) ;
Because the instances of a pointer to function are not
unipotent, it is necessary to pass an argument to the
constructor, to specify which one you want, but this is also
true for functional objects which aren't unipotent.
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34