tags:

views:

120

answers:

3

Hi, a C++ noob here. I am trying to tweak some code, with the following key lines (meaning they are not the only ones, but they are the only ones that should matter for this question). By the way, I am using Visual Studio 2010 C++ compiler on Windows.

CMap<ATL::CAtlString,LPCTSTR,UINT,UINT> mapForDuplicates; // "dict" definition
ATL::CAtlString strDescription = ... // let's just say it gets set to a value.
UINT nFound = 0; // What is this for???
BOOL bFound = mapForDuplicates.Lookup(strDescription, nFound);
mapForDuplicates[strDescription] = 1;

Now ... I really do not want to use the UINT here, as bool is all I really need. However, I could not figure out what all of the arguments for the CMap constructor really are. When using C#, all I have to specify is the type of the key and the type of the value. The fact that ATL::CAtlString does not match LPCSTR really confuses me. What exactly are KEY, ARG_KEY, VALUE, and ARG_VALUE? Why do I need all four and can all four be different? Thanks.

...
template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
class CMap : public CObject
...

Note: I could use std::map here instead(although I have not used it either); the only non-negotiable is ATL::CAtlString - I have to use this type. Let me know if you have questions.

+1  A: 

Start with the docs for the class =- there is a ref to a sample here too

http://msdn.microsoft.com/en-us/library/s897094z(VS.71).aspx

Steve Townsend
Thanks, I read this, and it was of some, but little help. I still do not understand why four parameters are needed, why and when the types can differ, and a clear example of usage. Having that would be much appreciated.
Hamish Grubijan
+1  A: 

IIRC the four args to the template are there so you can throw one type in and get another (const) type back. Here it throws in CAtlStrings, but it'll get back LPCTSTR. Often you just specify the same to types twice (e.g. int, int, float, float for a map of ints -> floats).

Grr, that extra L really irks me nowadays, it was great for 16-bit Windows but nowadays... PCSTR is all that's needed. 'L' is the useless appendix of Windows programming.

nFound is something coming out of the map, the map maps to UINT so nFound is a UINT.

Graham Perks
Thanks, this was helpful. When the types do differ, does it use a simple cast? Also, the `LPCSTR` thingy ... since I am not iterating over the keys, I should be able to change that to `ATL::CAtlString` as well, right?
Hamish Grubijan
It's fast for Atl to return you a PCTSTR from an AtlString. CMap doesn't let you edit the key, so it wants to pass you back something const. You could try making the 2nd arg a const AtlString, but a PCTSTR is just fine.
Graham Perks
A: 

By the way, the following pseudo-sample did the trick for me.

std::set<CAtlString> setOfDescriptions;
for each(...)
{
    CAtlString strDescription = GetDescription();
    if (setOfDescriptions.find(strDescription) != setOfDescriptions.end())
    {
        // Remove a duplicate.
    }

    setOfDescriptions.insert(strDescription); // Mark as seen.
}
Hamish Grubijan