This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: operator[] and std::map


>>> 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 at mcs dot le dot ac dot uk
  Maths and Computer Science,     http://www.mcs.le.ac.uk/~glaycock
  University of Leicester         phone:         (+44) 116 252 3902
  UK                              GPG:                     7D025C00


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]