|
Boost : |
Subject: Re: [boost] Re : BOOST_FOREACH like macro for map
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-10-20 17:28:36
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
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk