Re: Use of CString key in CMap
"Kjell Arne Johansen" <KjellArneJohansen@discussions.microsoft.com> wrote in
message news:46BAA990-8FDC-4ACF-8ECB-359879F03232@microsoft.com...
Hi
Thank you for your answer.
As I understand it a good haskey will make lookup very fast, maybe
dependent
of the string keys?
If they string keys are very much the same e.g. _T("abc1"), _T("abc2"),
_T("abc3") lookup will take longer time than if the string keys were
totaly
different?
Yes, the hash function is responsible for selecting e.g. which element of
the array to store the object into. Ideally for the set of keys you are
storing, the hash function will pick each element the same number of times
so that each element has the same number of objects saved in it. This
minimizes the average lookup time since the number of items to check in any
element is as small as possible.
Generally, the hash function does well about distributing similar strings as
per your example, e.g. "abc1" and "abc2" and "abc3" will probably all go to
different elements and be ideal. But I'm not sure if any of the MFC maps
will let you optimize the hash function.
Another question regading string keys and mfc supported maps:
What if I want to map string to int?
E.g.
CMap<CString, CString &, int, int&> m_map;
I also have a case where I need to lookup an int number using a string
key.
I have tried it using the current haskey.
Is that okay?
I could not find any mfc mapstringtoint.
No, there is no pre-built specialized class mapping a string to an int.
You'd use templates as you have done. But again, don't worry about the hash
function. You're optimizing something that you can assume does not need
optimizing unless you have concrete proof it is causing visible performance
impact. I've never had this be the case.
-- David