This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: Subtle MT problem with __gnu_cxx::hash_map
On Tue, Nov 23, 2004 at 10:02:19AM -0500, Paul Dubuc wrote:
> No, but operator[] always does a read, whether it inserts or not. So this is not
> a relevant comparison. Can't you see the ambiguity here? There is no analogy
> to operator[] because it is a find() which will insert and return an empty
> element is one is not already there.
If the function with the interface that you want is not present, then
just write it:
template<typename Container, typename KeyType, typename ResultType>
ResultType lookupKey(const Container& container, const KeyType& key,
const ResultType& defaultValue)
{
Container::const_iterator iter = container.find(key);
return iter == container.end() ? defaultValue : iter->second;
}
The above should be good for hash_map or map.