Last active
May 14, 2021 22:01
-
-
Save arnobaer/abfcdb68b26f4aee2c12c4cb2e4aac45 to your computer and use it in GitHub Desktop.
Yet another personal C++11 recipe book
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; }; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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