map::operator[]
is a little odd. It does this:
- Look for the key.
- If found, return it.
- If not, insert it and default-construct its associated value.
- Then return a reference to the new value.
Step 3 is incompatible with const
ness. Rather than have two differently-functioning operator[]
overloads, the language forces you to use map::find
for const
objects.
Alternately, one could argue, what would map::operator[] const
do if the argument is not in the map? Throw an exception? Undefined behavior? (After all, that's what vector::operator[]
does with an index out of bounds.) In any case, the problem is avoided with only a small inconvenience to us.
my_map.find(key)
returns my_map.end()
if the key
is not found.