[Cs3] consts with maps
Mikhail Nesterenko
mikhail at cs.kent.edu
Mon Sep 28 13:19:35 EDT 2015
CS3,
Here is the story. In a map, key is always implicitly const. Its modification is
not allowed.
map<int, double> mymap; // implies that the integer key is not modifiable
An attempt to change a key through iterator should fail as the
iterators const-ify reference to the key.
However,
map<const int, double> mymap; // is allowed, is somewhat redundant
// and is treated as a separate type
// the above map
Now, as far as value, it can be declared const
map<int, const double> mymap;
In which case, the value cannot be modified. Also, the map itself can
be declared const:
const map<int, double> mymap = {{0, 0.}, {1, 1.}};
In which case, the map cannot be modified altogether. There seems to
be a lot of frustrating on the internet since indexing operator
(that can potentially modify the map) is not allowed for constant
maps.
Thanks,
--
Mikhail
More information about the cs3
mailing list