如何将CString和std :: string std :: wstring相互转换? [英] How do you convert CString and std::string std::wstring to each other?

查看:153
本文介绍了如何将CString和std :: string std :: wstring相互转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CString 非常方便,而 std :: string 与STL容器更兼容.我正在使用 hash_map .但是, hash_map 不支持 CString 作为键,因此我想将 CString 转换为 std :: string .

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.

编写 CString 哈希函数似乎需要很多时间.

Writing a CString hash function seems to take a lot of time.

CString -----> std::string

我该怎么做?

std::string -----> CString:

inline CString toCString(std::string const& str)
{
    return CString(str.c_str()); 
}

我说得对吗?

还有更多问题:

如何从 wstring 转换为 CString ,反之亦然?

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());

这有什么问题吗?

此外,如何将 std :: wstring 转换为 std :: string ,反之亦然?

Additionally, how can I convert from std::wstring to std::string and vice versa?

推荐答案

根据 CodeGuru :

CString std :: string :

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

但: 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 :(来自

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).

换句话说,( CStringT 的字符专用化),即 CStringA wchar_t -specilization CStringW ,和 TCHAR -specialization CString 可以用 char 或宽字符构造,空终止(空终止在这里非常重要)字符串源.
Althoug
IInspectable 修改了空终止"部分

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 的转换构造函数带有一个明确的length参数.这也意味着您可以使用嵌入了 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 construct CStringT objects from std::string objects with embedded NUL characters.

这篇关于如何将CString和std :: string std :: wstring相互转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆