开发者

Help using MFC CMap (or std::map) please

a C++ noob here. I am trying to tweak some code, with the following key lines (meani开发者_StackOverflowng 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.


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.


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


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.
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜