Re: generalized STL set/map
On Apr 20, 10:29 pm, Arno <ascho...@think-cell.com> wrote:
I am sure this topic has been brought up before, but I have no good
understanding what the underlying design rationale of the current map/
set is, and the current design gives me a frequent headache when
A general combined key/value type + a key extractor function would
give you all the power of set/map, and much more, like putting keys
into member variables of the value, avoiding the ugly map pair, or
even calculating the key from the value on the fly.
For a set, the object itself is the key. I am not sure why you mention
that. With maps, yes that would be good to have, but it is a very
specialized container. Current std::map is the most generalized one
that it can be. If you have a specific way to set the map key - that's
a specialization. Generality comes, when you seperate the key from the
value and instead a pair can be inserted. I think you are wanting to
have something like KeyedCollection in C#.
If the extractor functor is required to be a unary_function, the
argument type can be used as the key_type, the return type as the
value_type, saving typing and ensuring template parameter consistency.
The comparator parameter would not have to be used as much, because
key_type::operator< can be defined appropriately instead, since
key_type's only purpose is comparison, not data storage.
I don't understand what you meant here but you might be wrong in
saying that key_type's only purpose is comparison. Many times, that
key is a part of the value that itself is an integral part data of the
data. For a map, that would be some unique identifier for the value
Are there any disadvantages to this design compared to the current set/
map? Is there a way to simulate what I want with the current design
that I overlooked?
There are no disadvantages - just that it is a specialized need. You
can always write a wrapper around std::map to do what you want and
localize generating keys, extracting keys and having to make pairs
from the value object encapsulation inside this wrapper.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]