CMap的简单应用示例
代码如下
#include "stdafx.h"
#include <afxtempl.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
CMap<CString, LPCTSTR, CString, LPCTSTR> map;
CString strKey;
CString strVal;
//添加
for (int i=0;i<5;i++)
{
strKey.Format(L"KEY%d",i);
strVal.Format(L"VAL%d",i);
map.SetAt(strKey,strVal);
}
//遍历
POSITION pos = map.GetStartPosition();
while (pos)
{
map.GetNextAssoc(pos,strKey,strVal);
wcout<<(LPCTSTR)strKey<<L" : "<<(LPCTSTR)strVal<<endl;
}
//查找
CString strFindKey(L"KEY3");
CString strFoundVal;
if (map.Lookup(strFindKey,strFoundVal))
{
wcout<<(LPCTSTR)strFindKey<<"--->"<<(LPCTSTR)strFoundVal<<endl;
}
system("pause");
return 0;
}
结果输出:

注:VS2005控制台程序应用MFC类,需做如下设置:


浙公网安备 33010602011771号