Is there a way to specify the default value std::map\'s operator[] returns when an key does not exist?
No, there isn't. The simplest solution is to write your own free template function to do this. Something like:
#include
#include
C++11 Update
Purpose: Account for generic associative containers, as well as optional comparator and allocator parameters.
template class C, typename K, typename V, typename... Args>
V GetWithDef(const C& m, K const& key, const V & defval)
{
typename C::const_iterator it = m.find( key );
if (it == m.end())
return defval;
return it->second;
}