본문 바로가기

Windows/MFC

CMap

Expand the Header Files folder, and open file StdAfx.h.
At the end of the file, type:
#include     // MFC templates

///////////////////////////////////////////////////////////////////////////////
CMap < class KEY, class ARG_KEY, class VALUE, class ARG_VALUE >

    KEY         Class of the object used as the key to the map.
    ARG_KEY     Data type used for KEY arguments; usually a reference to KEY.
    VALUE     Class of the object stored in the map.
    ARG_VALUE Data type used for VALUE arguments; usually a reference to VALUE.

    CMap is a dictionary collection class that maps unique keys to values.

    ==================================================
    Class Members
    ==================================================
    GetCount
        int GetCount( ) const;
    GetHashTableSize
        UINT GetHashTableSize( ) const;
    GetNextAssoc
        void GetNextAssoc( POSITION& rNextPosition, KEY& rKey, VALUE& rValue ) const;
    GetStartPosition
        POSITION GetStartPosition( ) const;
    InitHashTable
        void InitHashTable( UINT hashSize );
    IsEmpty
        BOOL IsEmpty( ) const;
    Lookup
        BOOL Lookup( ARG_KEY key, VALUE& rValue ) const;
    RemoveAll
        void RemoveAll( );
    RemoveKey
        BOOL RemoveKey( ARG_KEY key );
    SetAt
        void SetAt( ARG_KEY key, ARG_VALUE newValue );
    operator[]
        VALUE& operator[]( ARG_KEY key );
            VALUE    Template parameter specifying the type of the map value.
            ARG_KEY Template parameter specifying the type of the key value.
            key     The key used to retrieve the value from the map.



    ==================================================
    Create
    ==================================================
    typedef CMap CMapStringToMyStruct;
    CMapStringToMyStruct m_mapStringToMyStruct;
    CString m_sKey;

    ==================================================
    Insert
    ==================================================
    m_mapStringToMyStruct[m_sKey] = pMyStruct;

    ==================================================
    Iterate
    ==================================================
    CMapStringToMyStruct& map = m_mapStringToMyStruct;
    POSITION pos = map.GetStartPosition();
    while (pos != NULL)
    {
        map.GetNextAssoc(pos, m_sKey, pMyStruct);
    }

    ==================================================
    Find
    ==================================================
    CMyStruct* pMyStruct;
    CMapStringToMyStruct& map = m_mapStringToMyStruct;
    if (map.Lookup(m_sKey, pMyStruct) != TRUE)
    {
        AfxMessageBox(IDS_KEY_NOT_FOUND);
        return;
    }

    ==================================================
    Update
    ==================================================
    m_mapStringToMyStruct.SetAt(m_sKey, pMyStruct);

    CMyStruct* pTemp = m_mapStringToMyStruct[m_sKey];
    delete pTemp; // delete old value
    m_mapStringToMyStruct[m_sKey] = pMyStruct;

    ==================================================
    Delete
    ==================================================
    if (m_mapStringToMyStruct.RemoveKey(m_sKey) != TRUE)
    {
        AfxMessageBox("Key not removed");
        return;
    }

    POSITION pos = m_mapStringToMyStruct.GetStartPosition();
    while (pos != NULL)
    {
        DWORD m_sKey;
        CMyStruct* pMyStruct;
        pDoc->m_mapStringToMyStruct.GetNextAssoc(pos, m_sKey, pMyStruct);
        delete pMyStruct;
    }
    pDoc->m_mapStringToMyStruct.RemoveAll();

    ==================================================
    Serialize
    ==================================================
    m_mapStringToMyStruct.Serialize(ar);

'Windows > MFC' 카테고리의 다른 글

구조체 초기화 하는 방법..  (0) 2013.10.02
Debuging 디버깅 방법  (0) 2013.10.02
CList  (0) 2013.10.02
CArray 클래스...  (0) 2013.10.02
트레이아이콘 사라졌을 때 trayicon  (0) 2013.10.02