Skip to content

Instantly share code, notes, and snippets.

@addam
Last active May 19, 2017 07:28
Show Gist options
  • Save addam/b3dbc025da9be17437620e1137969ff4 to your computer and use it in GitHub Desktop.
Save addam/b3dbc025da9be17437620e1137969ff4 to your computer and use it in GitHub Desktop.
setdefault for c++, the missing Python::dict method
/// 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