您的位置:首页 > 编程语言 > C语言/C++

C++ String: How to convert between 'CString' and 'std::string'?

2009-07-08 14:38 513 查看
'CString' to 'std::string':

std::string cannot always construct from a LPCTSTR i.e. the code will fail for UNICODE builds.

So the following conversion will lead to error:

CString cs("Hello");
std::string s((LPCTSTR)cs);


As std::string can construct only from LPSTR / LPCSTR, a programmer who uses VC++ 7.x or better can utilize conversion classes such as CT2CA as an intermediary.

CString cs ("Hello");
// Convert a TCHAR string to a LPCSTR
CT2CA pszConvertedAnsiString (cs);
// construct a std::string using the LPCSTR input
std::string strStd (pszConvertedAnsiString);


'std::string' to 'CString':

std::string s("Hello");
CString cs(s.c_str());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: