Last active
May 19, 2017 07:28
-
-
Save addam/b3dbc025da9be17437620e1137969ff4 to your computer and use it in GitHub Desktop.
setdefault for c++, the missing Python::dict method
This file contains hidden or 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
/// setdefault(D, k, v) -> D.at(k), and set D[k]=v if k not in D | |
template<class T, class K, class V> | |
V& setdefault(T &map, const K &key, const V &default_value) | |
{ | |
if (!map.count(key)) { | |
map.insert({key, default_value}); | |
} | |
return map.at(key); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment