更新时间:2021-11-27 05:44:52
根据CodeGuru:
CString
到 std::string
:
CString cs("Hello");
std::string s((LPCTSTR)cs);
BUT: std::string
不能总是从 LPCTSTR
构造.即,UNICODE 构建的代码将失败.
BUT: std::string
cannot always construct from a LPCTSTR
. i.e. the code will fail for UNICODE builds.
由于 std::string
只能从 LPSTR
/LPCSTR
构造,使用 VC++ 7.x 或更高版本的程序员可以利用转换CT2CA
等类作为中介.
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
到 CString
:(来自 Visual Studio 的 CString 常见问题解答...)
std::string s("Hello");
CString cs(s.c_str());
CStringT
可以从字符或宽字符串构造.即它可以从char*
(即LPSTR
)或wchar_t*
(LPWSTR
)转换.
CStringT
can construct from both character or wide-character strings. i.e. It can convert from char*
(i.e. LPSTR
) or from wchar_t*
(LPWSTR
).
换句话说,char-specialization (of CStringT
) ie CStringA
, wchar_t
-specilization CStringW
,和 TCHAR
-specialization CString
可以从 char
或宽字符构造,空终止(空终止在这里非常重要) 字符串来源.
Althoug IInspectable 修改了null-termination"部分 在评论中:
In other words, char-specialization (of CStringT
) i.e. CStringA
, wchar_t
-specilization CStringW
, and TCHAR
-specialization CString
can be constructed from either char
or wide-character, null terminated (null-termination is very important here) string sources.
Althoug IInspectable amends the "null-termination" part in the comments:
不需要 NUL 终止.CStringT
具有采用显式长度参数的转换构造函数.这也意味着您可以使用嵌入 NUL
字符的 std::string
对象构造 CStringT
对象.
NUL-termination is not required.
CStringT
has conversion constructors that take an explicit length argument. This also means that you can constructCStringT
objects fromstd::string
objects with embeddedNUL
characters.