관리 메뉴

드럼치는 프로그래머

[C/C++] CMap 사용 예제 본문

★─Programing/☆─C | C++

[C/C++] CMap 사용 예제

드럼치는한동이 2011. 6. 13. 11:07

원본 : http://blog.daum.net/aswip/6957183

 

#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;
}




[출처] http://blog.naver.com/mllmaster?Redirect=Log&logNo=130048724359

Comments