I'm accessing a lot of maps in a non-performance critical piece of code. I don't want to write the usual find/!=end all the time to check for missing keys. I also don't want to use [] blindly and get default values. Is the following wrapper function smart or stupid? Is there a simpler way? Are there side effects I didn't consider?
template<typename M>
static typename M::mapped_type getMapValue(const M& m, typename M::key_type key) {
typename M::const_iterator it = m.find(key);
if (it != m.end()) {
return it->second;
} else {
std::cerr << "Key: " << key << " not found!" << std::endl;
std::cerr << "Returning default value." << std::endl;
return typename M::mapped_type();
}
}