I would find a typedef whose key is not const very useful. Does such a
typedef exist as a standard, or will it have to be
pair<mymap::key_type, mymap::mapped_type>
Cheers,
Shaun
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
A new typedef wouldn't do you much good. The reason the key is const is
because const keys are intrinsic to the correct internal workings of
std::map. If you did change the key, the map would (depending on exactly
what the new value was) stop working.
Neil Butterworth
The way that the pair is stored on the map depends on the value of the
key. There would need to be some mechanism that allows it to move
within the map for you to change the key. While this can be
implemented by properties (which do not exist as language feature in C+
+ but can be implemented via inner class + operator overload), this
would complicate the design of the container.
Sorry, I didn't explain myself fully. I'm looking for some syntatic
sugar to be able to modify the key of the pair before I insert it into
the map.
mymap::value_type v(foo, bar);
if (flag)
v.first = mogrify(v.first);
mymap.insert(v);
Cheers,
Shaun
What you are asking for violate the very basic of container of
entities which are 'sorted'. If your really wish to modify the key and
not some other portion of the data, then you will have to 'erase' and
re-'insert' the object.
If your key can be chopped to two portion - then you better use map,
in which the key is const and the value is modifiable.
A different way of implementation could be using a stl::set with a
different comperator then the default.
but usually you will endup with syntatic sugar for using 'set' instead
of map.
Note - not just the std::map require the key to be const but also all
impl of hash based containers.
bottom line - you will have to erase/insert if wish to stick to
destructive key.
Rabin
> Sorry, I didn't explain myself fully. I'm looking for some syntatic
> sugar to be able to modify the key of the pair before I insert it into
> the map.
>
> mymap::value_type v(foo, bar);
> if (flag)
> v.first = mogrify(v.first);
> mymap.insert(v);
How about this (untested code):
mymap::value_type v(foo, bar);
mymap.insert(
flag
? mymap::value_type( mogrify(v.first), v.bar )
: v
);
Neil Butterworth
still remember that what you ask does not make sense, assuming
modifing the key in insertion only -
MyMap mapInst;
Key key;
mapInst.insert(make_pair(key,val));
mapInst.find(key)->...// core dump since find should search for
mogrified(key)?
I don't wan to think about [] which involves find/insert...
Still, you may consider using a comperant which mimic the mogrifiy,
but keep the keys as originally stated.
Rabin
What about removing the old pair after you inserted the new one?
So don't put a key into value_type before knowing it's value? Are you
doing it this way because key so expensive to copy?
Alternatively, you could try using mutable keyword: if mogrify changes
only parts of the key that do not participate in map ordering, there
is no problem (aolthough it does not smell that good to me). As said,
you are going against the tide: key is const because otherwise,
conceptually, map container can't guarantee ordering.
Goran.
Use const_cast (on references).
Don't let your modification change the order!!!
How about this:
mymap::value_type v(flag ? mogrify(foo) : foo, bar);
mymap.insert(v);
or even
mymap.insert(mymap::value_type(flag ? mogrify(foo) : foo, bar));
(By the way, you cannot use the same name in front of ::value_type
and .insert.)
--
Seungbeom Kim