Re: map vs. set (stl)
On May 24, 1:57 am, Qwavel <qwa...@gmail.com> wrote:
On May 23, 7:39 pm, Markus Schoder <a3vr6dsg-use...@yahoo.de> wrote:
[...]
I'm using the STL that comes with MS VC8. I don't know if
this behavior conforms to the standard or not.
The current wording of the standard surprisingly _requires_
this behavior but there is a defect report pending (103)
that proposes to change this and make keys in associative
containers immutable.
Yikes! That change would break some of my code.
It's not so much a change as a clarification.
Does that mean that the best solution for me is to use
std::map<key,value>, in spite of the fact that I would then have two
copies of the key (since 'value' contains the key)?
It depends. If key isn't expensive to duplicate, why not? It
has the advantage that you can look up with just a key; you
don't have to construct a complete value.
The alterative would be a set of pointers:
std::set< value*, ValueKeyCmp >
where ValueKeyCmp is something like:
struct ValueKeyCmp
{
bool operator()( value const* a, value const* b ) const
{
return a->name() < b->name() ;
}
} ;
This would also allow using const members for the elements in
the key.
--
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