
20 Oct
2009
20 Oct
'09
9:28 p.m.
AMDG Stewart, Robert wrote:
That is horribly inefficient: O(N). Try this instead (untested):
template <class T, class U> U const & get_value(std::map<T,U> const & _map, T const & _key) { typename std::map<T,U>::const_iterator it(_map.find(_key)); if (it != _map.end()) { return it->second; } throw std::domain_error("key not found"); }
That is O log(N).
Wouldn't it be better to return a null pointer instead of throwing an exception? In Christ, Steven Watanabe