Re: Use of CString key in CMap
David Ching wrote:
"David Wilkinson" <no-reply@effisols.com> wrote in message
news:egZLjfxlIHA.4480@TK2MSFTNGP03.phx.gbl...
This may be a good article, but for me it simply illustrates how much
easier std::map is to use than CMap. And I still don't understand the
purpose of the duplicated template arguments.
std::map<CString, int> just works. No muss, no fuss. With an MFC class as
the key, even.
For me, It just seems the MFC classes follow the spirit of how these
collections are supposed to work... I mean, a map is another name for a
dictionary. CMap::Lookup() tells you whether your query is successful or
not instead of stupidly manufacturing a default object if you reference one
that is not there. What Webster's dictionary do you know that if you look
up a word that isn't in there, it creates an empty one on the fly,
corrupting your nice dictionary?
David:
AFAICT, operator [] has the same behavior in std::map and CMap.
The CMap documentation says that it "cannot" be used as an r-value, which is
nonsense. What they mean is, it "should not" be used as an r-value if you do not
want a default value inserted if the key is not found.
std::map::find() is like CMap::Plookup(). The former returns an iterator, while
the latter returns a pointer. Same thing, conceptually. Dereferencing either
gives an element of the map, which is a pair in both cases.
There is no analog of CMap::Lookup() in std::map; you have to test the result of
find(). No big deal.
--
David Wilkinson
Visual C++ MVP