Re: Use of CString key in CMap
"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?
I think STL also has some API's specifically like Lookup() but for me I
could not even figure out how to use them when I first looked at the public
functions. You had to tell me! :-O Not to mention that STL code is truly
ugly and not very readable. Oh well, what's intuitve to someone is not for
others. I will say that thankfully the .NET collections share the same
spirt of the MFC collections, and these are truly easy to use and seem to
have the benefit of STL (things like nesting, etc.).
Actually, I think the "correct" type for the second and fourth template
parameter in CMap is "const reference" (this is the default type of the
second parameter in recent versions of CArray and CList). It seems that
CMap<LPCTSTR, const LPCTSTR&, CString, const CString&> stringMap;
is allowed. [I didn't test it, but it compiles.]
CMap<CString, const CString&, CString, const CString&> stringMap;
will also work, but you have to provide your own hash function. You would
think that if MFC had provided a hash specializations for LPCTSTR, they
would have done it for CString also...
The CodeProject article said the 2nd and 4th arguments were used throughout
the implementation, but really the true arguments were the 1st and 3rd ones.
I'm not sure the history of this. Perhaps the reason why MFC has pre-made
CStringTo<many things> classes is due to there is no easy way to generate a
HashKey from the object.
Thanks,
David