这个算不算啊:(应该算吧)MSDN:CMap is a dictionary collection class that maps unique keys to values. Once you have inserted a key-value pair (element) into the map, you can efficiently retrieve or delete the pair using the key to access it. You can also iterate over all the elements in the map.A variable of type POSITION is used for alternate access to entries. You can use a POSITION to "remember" an entry and to iterate through the map. You might think that this iteration is sequential by key value; it is not. The sequence of retrieved elements is indeterminate.Certain member functions of this class call global helper functions that must be customized for most uses of the CMap class. See Collection Class Helpers in the Macros and Globals section of the MFC Reference.CMap incorporates the IMPLEMENT_SERIAL macro to support serialization and dumping of its elements. Each element is serialized in turn if a map is stored to an archive, either with the overloaded insertion (<<) operator or with the Serialize member function. If you need a diagnostic dump of the individual elements in the map (the keys and the values), you must set the depth of the dump context to 1 or greater.When a CMap object is deleted, or when its elements are removed, the keys and values both are removed.Map class derivation is similar to list derivation. See the article Collections for an illustration of the derivation of a special-purpose list class.
定义: CMap<int,int,CPoint,CPoint> myMap(16); 赋值: for (int i=0;i < 10;i++) myMap.SetAt( i, CPoint(i, i) );
CMap<int,int,CPoint,CPoint> myMap(16);
赋值:
for (int i=0;i < 10;i++)
myMap.SetAt( i, CPoint(i, i) );
索引 值
0 -> (0,0)
1 -> (1,1)
2 -> (2,2)
3 -> (3,3)
4 -> (4,4)
5 -> (5,5)
6 -> (6,6)
7 -> (7,7)
8 -> (8,8)
9 -> (9,9)
CMap<int, int, CString, CString> m_cMap;m_cMap.SetAt(9923033, "张三");
m_cMap.SetAt(9826033, "张A");
m_cMap.SetAt(9923063, "张B");
m_cMap.SetAt(9923093, "张C");CString strName;
m_cMap.LookUp(9923063, strName);
myMap.Lookup(3, point);<<执行后point为(3,3)
先执行myMap.SetAt(3, CPoint(3,3));
后再执行myMap.SetAt(3, CPoint(4,4));会覆盖前面的,即同一关键字的只能有一个值myMap.Lookup(3,point);取出的值将是(4,4)
MAP的声明:
加上<afxtempl.h>
typedef CMap<DWORD,DWORD,CMyStruct*,CMyStruct*&> CMapDWordToMyStruct;
以后直接用CMapDWordToMyStruct进行声明,方便不少。 MAP的初始化:
变量定义在Doc中,在OnNewDocument()中进行第一次的初始化。
CMyStruct* pMyStruct2 = new CMyStruct();
pMyStruct2->m_int = 2468;
pMyStruct2->m_float = 24.68f;
pMyStruct2->m_str.LoadString(IDS_INITIAL_STRING);
m_mapDWordToMyStruct[100] = pMyStruct2;//是这样插入数据的。 视图启动的时候做这些事情:
DWORD dwKey;
CMyStruct* pMyStruct;
m_ctlList.ResetContent();
CMapDWordToMyStruct& map = GetDocument()->m_mapDWordToMyStruct;
POSITION pos = map.GetStartPosition();
while (pos != NULL)
{
map.GetNextAssoc(pos, dwKey, pMyStruct);
AddMapEntryToListBox(dwKey, pMyStruct);
}
//以上代码遍历读取MAP的数据并送到控件中进行显示。 添加和替换操作:
// Add or replace entry in the listbox
int nSel = FindKeyInListBox(m_dwKey);//从控件中得到Key
if (nSel != LB_ERR)
m_ctlList.DeleteString(nSel);//如果存在在控件中删除记录
CMyStruct* pMyStruct = ConstructMyStructFromView();//从控件数据新建结构
AddMapEntryToListBox(m_dwKey, pMyStruct);//加入到列表控件 // Add or update association in the CMap
CMyStruct* pTemp;
if( GetDocument()->m_mapDWordToMyStruct.Lookup( m_dwKey, pTemp ) )
delete pTemp; // delete old value//查找目标,如果存在则删除,很方便的操作,不是吗?
GetDocument()->m_mapDWordToMyStruct[m_dwKey] = pMyStruct;//添加记录 全部删除操作:
CCollectDoc* pDoc = GetDocument();
POSITION pos = GetDocument()->m_mapDWordToMyStruct.GetStartPosition();
while (pos != NULL)
{
DWORD dwKey;
CMyStruct* pMyStruct;
pDoc->m_mapDWordToMyStruct.GetNextAssoc(pos, dwKey, pMyStruct);
delete pMyStruct;
}
pDoc->m_mapDWordToMyStruct.RemoveAll();
m_ctlList.ResetContent(); 删除操作:
if (UpdateData() != TRUE)
return; CMyStruct* pMyStruct;
CMapDWordToMyStruct& map = GetDocument()->m_mapDWordToMyStruct;
if (map.Lookup(m_dwKey, pMyStruct) != TRUE)
{
AfxMessageBox(IDS_KEY_NOT_FOUND);
return;
} if (m_int != pMyStruct->m_int
|| m_float != pMyStruct->m_float
|| m_str != pMyStruct->m_str)
{
UpdateViewFromMyStruct(pMyStruct);
AfxMessageBox(IDS_KEY_NOT_FOUND_CHOOSE_REMOVE_AGAIN);
return;
} // Remove assocation from the listbox
int nSel = FindKeyInListBox(m_dwKey);
ASSERT(nSel != LB_ERR);
m_ctlList.DeleteString(nSel); // Remove the association from the CMap
map.RemoveKey(m_dwKey); // Delete CMyStruct
delete pMyStruct; 查找操作:
if (UpdateData() != TRUE)
return; CMyStruct* pMyStruct;
if (GetDocument()->m_mapDWordToMyStruct.Lookup(m_dwKey, pMyStruct) != TRUE)
{
AfxMessageBox(IDS_KEY_NOT_FOUND);
return;
} UpdateViewFromMyStruct(pMyStruct); 这里主要是CMap在文档视图中的用法,可以看出,微软的代码写的非常严密,安全检查意识很好,函数的封装比较得体。