Skip to content

Instantly share code, notes, and snippets.

@arnobaer
Last active May 14, 2021 22:01
Show Gist options
  • Save arnobaer/abfcdb68b26f4aee2c12c4cb2e4aac45 to your computer and use it in GitHub Desktop.
Save arnobaer/abfcdb68b26f4aee2c12c4cb2e4aac45 to your computer and use it in GitHub Desktop.
Yet another personal C++11 recipe book
template<typename T>
const typename T::mapped_type& at(const T& m, const typename T::key_type& k, const typename T::mapped_type& d)
{
try { return m.at(k); } catch (const std::out_of_range&) { return d; };
}
template<typename T>
const typename T::mapped_type& get(const T& m, const typename T::key_type& key, const typename T::mapped_type& def)
{
typename T::const_iterator it = m.find(key);
if (it == std::end(m))
return def;
return it->second;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment