operator[] and std::map

Gilbert Laycock gtl1@mcs.le.ac.uk
Fri Mar 21 13:36:00 GMT 2003


>>> Gawain Bolton writes:

> Gilbert Laycock wrote:
>> Should it be possible to use operator[] from std::map in a const
>> context?

> No, there cannot be a const version of operator[] for std::map
> since, if the key passed is not already present in the map, it will
> be added with an associated value constructed using value_type's
> default constructor.

OK, that makes sense.

> What you must do is use "find()" and, if the key is present in the map,
> check the associated value.

This is what I did to get my code working.

Thanks for the swift response.

-- 

  Gilbert Laycock                 email:          gtl1@mcs.le.ac.uk
  Maths and Computer Science,     http://www.mcs.le.ac.uk/~glaycock
  University of Leicester         phone:         (+44) 116 252 3902
  UK                              GPG:                     7D025C00



More information about the Libstdc++ mailing list