반응형

STL과 MFC 클래스 간의 변환 함수 : 문자열(string), 맵(map), 리스트(list)

1. std::stringCString

std::stringCString

STL의 std::string을 MFC의 CString으로 변환하는 함수를 정의합니다.

#include <afxstr.h>  // CString 사용
#include <string>    // std::string 사용

CString StdStringToCString(const std::string& str) {
#ifdef UNICODE
    return CString(CA2W(str.c_str()));  // 멀티바이트를 유니코드로 변환
#else
    return CString(str.c_str());
#endif
}

CStringstd::string

MFC의 CStringstd::string으로 변환하는 함수를 정의합니다.

#include <afxstr.h>  // CString 사용
#include <string>    // std::string 사용

std::string CStringToStdString(const CString& cstr) {
#ifdef UNICODE
    return std::string(CW2A(cstr));  // 유니코드에서 멀티바이트로 변환
#else
    return std::string(cstr);
#endif
}

2. std::mapCMap

std::mapCMap

STL의 std::map을 MFC의 CMap으로 변환하는 함수를 정의합니다.

#include <afx.h>     // CMap 사용
#include <map>       // std::map 사용
#include <string>    // std::string 사용

void StdMapToCMap(const std::map<int, std::string>& stdMap, CMap<int, int, CString, LPCTSTR>& cMap) {
    for (const auto& pair : stdMap) {
#ifdef UNICODE
        cMap.SetAt(pair.first, CA2W(pair.second.c_str()));  // 멀티바이트를 유니코드로 변환
#else
        cMap.SetAt(pair.first, pair.second.c_str());
#endif
    }
}

CMapstd::map

MFC의 CMapstd::map으로 변환하는 함수를 정의합니다.

#include <afx.h>     // CMap 사용
#include <map>       // std::map 사용
#include <string>    // std::string 사용

void CMapToStdMap(const CMap<int, int, CString, LPCTSTR>& cMap, std::map<int, std::string>& stdMap) {
    POSITION pos = cMap.GetStartPosition();
    int key;
    CString value;

    while (pos != nullptr) {
        cMap.GetNextAssoc(pos, key, value);
#ifdef UNICODE
        stdMap[key] = CW2A(value);  // 유니코드에서 멀티바이트로 변환
#else
        stdMap[key] = std::string(value);
#endif
    }
}

3. std::listCList

std::listCList

STL의 std::list를 MFC의 CList로 변환하는 함수를 정의합니다.

#include <afxtempl.h>  // CList 사용
#include <list>        // std::list 사용

void StdListToCList(const std::list<int>& stdList, CList<int, int>& cList) {
    for (int value : stdList) {
        cList.AddTail(value);
    }
}

CListstd::list

MFC의 CListstd::list로 변환하는 함수를 정의합니다.

#include <afxtempl.h>  // CList 사용
#include <list>        // std::list 사용

void CListToStdList(const CList<int, int>& cList, std::list<int>& stdList) {
    POSITION pos = cList.GetHeadPosition();
    while (pos != nullptr) {
        int value = cList.GetNext(pos);
        stdList.push_back(value);
    }
}

이제 위의 함수를 이용해 각 변환을 간단히 호출할 수 있습니다.

사용 예제

#include <iostream>

int main() {
    // std::string to CString
    std::string stdStr = "Hello, CString!";
    CString cstr = StdStringToCString(stdStr);
    std::wcout << (LPCTSTR)cstr << std::endl;

    // CString to std::string
    CString mfcStr = _T("Hello, std::string!");
    std::string convertedStr = CStringToStdString(mfcStr);
    std::cout << convertedStr << std::endl;

    // std::map to CMap
    std::map<int, std::string> stdMap = {{1, "One"}, {2, "Two"}};
    CMap<int, int, CString, LPCTSTR> cMap;
    StdMapToCMap(stdMap, cMap);

    // CMap to std::map
    std::map<int, std::string> convertedMap;
    CMapToStdMap(cMap, convertedMap);
    for (const auto& pair : convertedMap) {
        std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;
    }

    // std::list to CList
    std::list<int> stdList = {1, 2, 3};
    CList<int, int> cList;
    StdListToCList(stdList, cList);

    // CList to std::list
    std::list<int> convertedList;
    CListToStdList(cList, convertedList);
    for (int value : convertedList) {
        std::cout << value << " ";
    }
    std::cout << std::endl;

    return 0;
}

각 함수는 인수로 std 또는 MFC 컬렉션을 받아 해당 변환된 컬렉션을 반환하는 방식으로 구성되어, 필요할 때마다 함수 호출로 간단히 변환할 수 있습니다.

728x90
반응형

+ Recent posts