If you try to access a key value using indexing operator []
, then 2 things can happen :
- The map contains this key. So it will return the corresponding key value
- The map doesn't contain the key. In this case it will
automatically add a key
to the map with key value null
.
As 'b'
key is not in your map so it will add this key with value ""
(empty string) automatically and it will print this empty string.
And here map size will increase by 1
So to look-up for a key you can use .find()
, which will return map.end()
if the key is not found.
And no extra key will be added automatically
And obviously you can use []
operator when you set a value for a key