Here's a generic function that looks up a key in a map and returns the corresponding value, or a default value if the key is not found. You can specify the default to use, otherwise it will use the "default default" which is to default construct the data type. It never inserts anything:
Code:
template <typename C>
typename C::data_type find_or_default(const C &container_p, typename C::key_type key_p,
typename C::data_type default_p = typename C::data_type ())
{
typename C::const_iterator i = container_p.find(key_p);
if(i == container_p.end()) return default_p;
return i->second;
}
It should work on any mapping type. Have not compiled or tested it. If you data_type does not have a default constructor, you will have to remove the default-default argument (too many occurrences of the word "default!")