Click here to Skip to main content
15,886,664 members
Articles / Desktop Programming / MFC

Quick and Dirty Collection Class Notes

Rate me:
Please Sign up or sign in to vote.
4.11/5 (10 votes)
28 Nov 2000 97K   1K   33  
An article describing MFC Collection Classes
Expand the Header Files folder, and open file StdAfx.h.
At the end of the file, type:
#include <afxtempl.h>    // MFC templates

///////////////////////////////////////////////////////////////////////////////
CTypedPtrMap < class BASE_CLASS, class KEY, class VALUE >

    BASE_CLASS      Base class of the typed pointer map class;
                    must be a pointer map class
                    (CMapPtrToPtr, CMapPtrToWord, CMapWordToPtr, or CMapStringToPtr).
    KEY             Class of the object used as the key to the map.
    VALUE           Class of the object stored in the map.

    The CTypedPtrMap class provides a type-safe �wrapper� for objects of the
    pointer-map classes CMapPtrToPtr, CMapPtrToWord, CMapWordToPtr, and
    CMapStringToPtr. When you use CTypedPtrMap, the C++ type-checking facility
    helps eliminate errors caused by mismatched pointer types.

    ==================================================
    Class Members
    ==================================================
    GetNextAssoc
        void GetNextAssoc( POSITION& rPosition, KEY& rKey, VALUE& rValue ) const;
    Lookup
        BOOL Lookup( BASE_CLASS::BASE_ARG_KEY key, VALUE& rValue ) const;
    RemoveKey
        BOOL RemoveKey( KEY key );
    SetAt
        void SetAt( KEY key, VALUE newValue );
    operator[]
        VALUE& operator [ ]( BASE_CLASS::BASE_ARG_KEY key );
            VALUE      Template parameter specifying the type of values stored in this map.
            BASE_CLASS Template parameter specifying the base class of this map�s class.
            key        The key of the element to be looked up or created in the map.

    ==================================================
    Create
    ==================================================
    typedef CTypedPtrMap<CMapStringToOb,CString,CMyObject*> CMapStringToMyObject;
    CMapStringToMyObject m_mapStringToMyObject;

    ==================================================
    Insert
    ==================================================
    CString strKey, strValue;
    strKey.LoadString(IDS_INITIAL_KEY);
    CMyObject* pMyObject2 = new CMyObject();
    pMyObject2->m_int = 1357;
    pMyObject2->m_float = 13.57f;
    pMyObject2->m_str.LoadString(IDS_INITIAL_STRING);
    m_mapStringToMyObject[strKey] = pMyObject2;

    ==================================================
    Iterate
    ==================================================
    CString strKey;
	CMyObject* pMyObject;
    CMapStringToMyObject& map = m_mapStringToMyObject;
	POSITION pos = map.GetStartPosition();
	while (pos != NULL)
	{
		map.GetNextAssoc(pos, strKey, pMyObject);
    }

    ==================================================
    Find
    ==================================================
    CString str;
    CMyObject* pMyObject;
    if (m_mapStringToMyObject.Lookup(strKey, pMyObject) != TRUE)
    {
		AfxMessageBox(IDS_KEY_NOT_FOUND);
		return;
	}

    ==================================================
    Update
    ==================================================
    CMyObject* pTemp = m_mapStringToMyObject[m_strKey];
	delete pTemp; // delete old value
    m_mapStringToMyObject[m_strKey] = pMyObject;

    ==================================================
    Delete
    ==================================================
    pos = m_mapStringToMyObject.GetStartPosition();
	while (pos != NULL)
	{
		CString str;
		CMyObject* pMyObject;
		m_mapStringToMyObject.GetNextAssoc(pos, str, pMyObject);
		delete pMyObject;
	}
	m_mapStringToMyObject.RemoveAll();

    ==================================================
    Serialize
    ==================================================
    m_mapStringToMyObject.Serialize(ar);




By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions