1

I am new in C++ and I need your helps.

My goal is to compare some Strings in c++ and store the identical ones. To do this. I hash the Strings to UINT then I will check The UINTS and store the same UINTS. For example.. I have 2000 Documents of texts, each of them has 2 lines. With a hash map I hash them to bunch of UINTS. then I store the same UINTS together. Finally I want to retrieve the identical documents. So, I use the Cmap. which maps the UINTS to their documents. I will do this procedure to increase the memory efficency.

I am using this code:

CString keyExample("This is a sample text");
LPCTSTR lpStr = (LPCTSTR)keyExample;
CMapStringToOb hashObject;
UINT keyExampleTemp=hashObject.HashKey(lpStr);
cout<< keyExampleTemp<<endl;

CMap<UINT, UINT, CString, CString*> mymap;
CString value=mymap[keyExampleTemp];
cout<<value;

If I comment the last 3 lines, I will get a UINT for "This is a sample text", but if I uncomment the last 3 lines, I will get this error which means the problem is with cmap

error C2664: 'CMap<KEY,ARG_KEY,VALUE,ARG_VALUE>::SetAt' : cannot convert parameter 2 from 'CString' to 'ATL::CStringT<BaseType,StringTraits> '

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Bipario
  • 221
  • 1
  • 4
  • 14
  • 2
    If you're new to C++ then do yourself a favor and stay as far away as possible from the archaic C-with-classes relic that is MFC. – ildjarn Dec 27 '11 at 22:38
  • See as well this post concerning how to define a `CMap` that is using `CString` as well as what the `CMap` template arguments mean. https://stackoverflow.com/questions/32019236/using-mfc-cmap-for-cstring-int-pairs which suggests using `LPCTSTR` rather than `CString *` and why with link to supporting article. – Richard Chambers Apr 18 '18 at 22:02

1 Answers1

4

CMap::operator []() is used for setting elements, not retrieving them. Use the Lookup() function instead.

EDIT:

Assuming I'm understanding your comment below, the following should work:

CString keyExample("This is a sample text");
LPCTSTR lpStr = (LPCTSTR)keyExample;
CMapStringToOb hashObject; // just used for hash generation...
UINT keyExampleTemp = hashObject.HashKey(lpstr);

CMap<UINT, UINT &, CString, LPCTSTR> mymap;
mymap[keyExampleTemp] = keyExample; // this stores the string in mymap using a key generated by the hash function above...

CString keyRetrieved;
mymap.Lookup(keyExampleTemp, keyRetrieved); // if this call returns 0, no element was found, otherwise keyRetrieved will contain "This is a sample text".
mwigdahl
  • 16,268
  • 7
  • 50
  • 64
  • I am hashing the "CString KeyExample" to "UINT KeyExampleHash" with a hash function called CMapStringToOb::HashKey. Then I want to retrieve it with cmap. How can I use The lookup here? Could you please write it for me? I am a bit confused in these classes !! – Bipario Dec 27 '11 at 22:49
  • 1
    Are you saying you want to store the `KeyExample` string in the map, referenced by the `KeyExampleHash` value generated by `HashKey`? – mwigdahl Dec 27 '11 at 23:06
  • Thanks for your answer. I got an error. the error is related to "&KeyRetrieved" in the last line. It says "IntelliSense: initial value of reference to non-const must be an lvalue" – Bipario Dec 28 '11 at 01:14
  • I always get confused with how `CMap` works with `CString`s in different versions of MFC. I've edited the code block; give it another try. – mwigdahl Dec 28 '11 at 15:40