반응형
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
}
MFC의 CString
을 std::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
}
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
}
}
MFC의 CMap
을 std::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
}
}
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);
}
}
MFC의 CList
를 std::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
반응형
'C C++' 카테고리의 다른 글
SOCI : C++ 데이터베이스 액세스 라이브러리 (0) | 2024.10.31 |
---|---|
eventpp : 이벤트 디스패처 및 콜백 목록을 위한 C++ 라이브러리 (0) | 2024.10.30 |
Nameof C++ : 변수, 유형, 함수, 매크로 및 열거형의 이름을 간단히 가져오는 Modern C++의 Nameof 연산자 (0) | 2024.10.29 |
EigenRand : Eigen을 위한 가장 빠른 C++11 호환 난수 분포 생성기 (0) | 2024.10.29 |
Beast : C++11 및 Boost.Asio를 기반으로 구축된 HTTP 및 WebSocket (0) | 2024.10.27 |