댓글 쓰기 권한이 없습니다. 로그인 하시겠습니까?
|
MFC
2010.06.23 20:02
[Collection] CMap 템플릿 클래스 사용하기
조회 수 64713 댓글 0
CMap은 사전이라고 생각하시면 됩니다.
#include "stdafx.h"
#include <afxtempl.h>
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nValue = 0;
CString strKey;
POSITION pos = NULL;
CMap<CString, LPCSTR, int, int> m;
/* 1. add key and value */
m.SetAt("AAA", 111);
m.SetAt("ABC", 123);
/* 2. lookup key 'AAA' */
if ( m.Lookup("AAA", nValue) )
printf("find 'AAA' = %d\n", nValue);
else
printf("no such key\n");
/* 3. replace key and value */
m.SetAt("AAA", 333);
if ( m.Lookup("AAA", nValue) )
printf("find 'AAA' = %d\n", nValue);
else
printf("no such key\n");
/* 4. iterate map */
pos = m.GetStartPosition();
while ( pos != NULL )
{
m.GetNextAssoc(pos, strKey, nValue);
printf("%s = %d\n", strKey.operator LPCTSTR(), nValue);
}
/* 5. remove 'AAA' key */
m.RemoveKey("AAA");
/* 6. iterate map after removing 'AAA' key */
pos = m.GetStartPosition();
while ( pos != NULL )
{
m.GetNextAssoc(pos, strKey, nValue);
printf("%s = %d\n", strKey.operator LPCTSTR(), nValue);
}
/* 7. print map count */
printf("a saved number of key in map is %d\n", m.GetCount());
/* 8. remove all existing key pair */
m.RemoveAll();
printf("a saved number of key in map is %d\n", m.GetCount());
return 0;
}
Dreamy의 코드 스크랩내가 모으고 내가 보는
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Designed by sketchbooks.co.kr / sketchbook5 board skin
Sketchbook5, 스케치북5
Sketchbook5, 스케치북5
Sketchbook5, 스케치북5
Sketchbook5, 스케치북5