CMap类的用法

定义:
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)

 

//关键字为int型 比如学号
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);  

参考:
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在文档视图中的用法,可以看出,微软的代码写的非常严密,安全检查意识很好,函数的封装比较得体。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值