⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmap.txt

📁 MFC集合类使用说明,简单明了,通俗易懂
💻 TXT
字号:
Expand the Header Files folder, and open file StdAfx.h.
At the end of the file, type:
#include <afxtempl.h>    // 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<CString, LPCSTR, CMyStruct*,CMyStruct*&> 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);



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -