본문 바로가기

Windows/MFC

template

[code]꽤나 매력적인 template 형을 함수는 그대로 두고 형만 내가 마음대로 지정해서 동일 사용할 수 있는... 어떻게 생각해보면 오버로딩 해야될 것을 한번에 해주게 되는.... 것이라고 우선 생각해두면 될 것 같다. 유지보수 비용이 탁월하게 줄겠지?

Example : 중복되지 않는 렌덤 번호를 생성하기

int nMax = nCntImage * nCntImage;
int nRand;
m_pnSliceImage = new int[nMax];
memset(m_pnSliceImage, 0, sizeof(int) * nMax);
int nBase = 0;

do
{
    nRand = CCommon::GetRand(nMax);
    TRACE(L"CREATE RAND : %d\n", nRand);
    if(!CCommon::CheckIsData<int>(m_pnSliceImage, nRand, nMax))
    {
        continue;
    }
    else
    {
        m_pnSliceImage[nBase++] = nRand;
        TRACE(L"save data \n");
    }

    TRACE(L"------------------------- %d, %d\n", nMax, nBase);
}
while(nMax != nBase);
[/code]

static int GetRand(int nRange)
{
    // 0은 렌덤 값에서 제외한다.
    static bool bInit = false;

    if (!bInit)
    {
        bInit = true;
        srand((unsigned)GetTickCount());
    }
    
    return (rand() % (nRange+1));
};

template <typename T>
static bool CheckIsData(T* pArray, T tValue, int nMax)
{
    do
    {
        // 배열 안에 값이 존재하면 false날려주신다.

        TRACE(L"Check it : %d %d %d\n", *pArray, tValue, nMax);
        if (*pArray == tValue)
        {
            return false;
        }

        pArray++;
    }
    while(--nMax);

    return true;
};

'Windows > MFC' 카테고리의 다른 글

SetCursor 커서 가져오기  (0) 2013.10.02
autorun 실행 script  (0) 2013.10.02
dll 생성 AFX_EXT_CLASS  (0) 2013.10.02
동적 메모리 생성  (0) 2013.10.02
CMemDC 사용법 (double buffering)  (0) 2013.10.02