CString is quite handy, while std::string is more compatible with STL container. I am using hash_map. However, hash_map does not support CStrings as keys, so I want to convert the CString into a std::string.
Writing a CString hash function seems to take a lot of time.
CString -----> std::string
How can I do this?
std::string -----> CString: inline CString toCString(std::string const& str) { return CString(str.c_str()); }
Am I right?
EDIT:
Here are more questions:
How can I convert from wstring to CString and vice versa?
// wstring -> CString std::wstring src; CString result(src.c_str()); // CString -> wstring CString src; std::wstring des(src.GetString());
Is there any problem with this?
Additionally, how can I convert from std::wstring to std::string and vice versa?
According to CodeGuru:
CStringtostd::string:BUT:
std::stringcannot always construct from aLPCTSTR. i.e. the code will fail for UNICODE builds.As
std::stringcan construct only fromLPSTR/LPCSTR, a programmer who uses VC++ 7.x or better can utilize conversion classes such asCT2CAas an intermediary.std::stringtoCString: (From Visual Studio’s CString FAQs…)CStringTcan construct from both character or wide-character strings. i.e. It can convert fromchar*(i.e.LPSTR) or fromwchar_t*(LPWSTR).In other words, char-specialization (of
CStringT) i.e.CStringA,wchar_t-specilizationCStringW, andTCHAR-specializationCStringcan be constructed from eithercharor wide-character,null terminated (null-termination is very important here)string sources.Althoug IInspectable amends the ‘null-termination’ part in the comments: