Re: Use of CString key in CMap
"Kjell Arne Johansen" <KjellArneJohansen@discussions.microsoft.com> ha
scritto nel messaggio
I have a question about the use of CString as a key in a CMap. Is that
To add to David's correct answer of using CMapStringToOb, I would also
suggest to use std::map.
One of the reasons I like std::map better than CMap is that you don't need
to provide a custom hash-key generator for std::map (or, at least, I never
had to do that). Instead, it seems that you must provide that with CMap (at
least for CString key...).
Moreover, with std::map you can have type-safety (because you can specify
the exact template value type T), instead with CMapStringToOb the "value"
type (in "key -> value" association) is a CObject *, so you kind of loose
type safety (or at least, it is not as robust and strict as in std::map).
Moreover, if you compare std::map and CMap definitions, you can note how
std::map is simpler (you just need to specify two template types: the key
type and value type - you can specify also other details, like compare
function and allocator, but default ones are also provided):
// std::map< Key, Value >
// (default compare function and allocator used)
std::map< CString, CSomeObject > m_map;
// (You must specify 4 types...)
CMap<CString, CString &, csomecobject, csomecobject &> m_map csomecobject;