Originally Posted by
EvilGuru
Hi,
In my application I currently use std::map. However, I have found no way to solve the following problems: when an invalid key is passed to operator[] it inserts a new element into the map.
Don't use operator[] -- use find(). If the key does not exist, it will return end(). We had another discussion on this topic just this morning.
Example:
Code:
map_type::const_iterator i = my_map.find(some_key);
if(i == my_map.end())
{
// Key not found
}
else
{
// Key found -- the corresponding item is in:
i->second;
}
You might encapsulate this in a lookup function which returns the default value (0) if the key isn't found.