Re: std::hash_map question (was re: replacing unsigned char * with CString)
"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message
news:sldab25ti2d1bouq0htdi5q82hr1ftjovi@4ax.com...
I wouldn't call that a query; it's really an access in service of a query.
If you want to determine if an item is present, use the find member, e.g.
if (m.find(x) != m.end()) ...
Note that the standard associative containers allow you to say the
following when m[x] doesn't exist:
m[x] = y;
f(m[x]);
Rather than throw an exception if m[x] doesn't exist, which is the only
reasonable alternative, the associative containers add a default object to
the container. If you use this feature, your mapped type must have a
default ctor.
OK, thanks Doug. It makes sense when you consider container's can contain
objects as well as pointers, and there is no reasonable way to represent a
NULL object. Still, it's a pretty bad behavior to have an insertion done
behind your back.
The thing I found most surprising about the maps is that their insert
function doesn't update an existing item. Instead, if the item exists,
they
leave the map unchanged, signifying that the insertion didn't take place
by
returning a std::pair whose "second" member is false.
Yes, this also threw me for a loop. Obviously, the user wants the existing
item to be updated. I don't know what kind of logic the authors of this
library were following.
-- David