如何将CString和:: std :: string :: std :: wstring转换为对方?
 CString非常方便,而std::string与STL容器更兼容。 我正在使用hash_map 。 但是, hash_map不支持CString作为键,所以我想将CString转换为std::string 。 
 编写一个CString哈希函数似乎花费了很多时间。 
 CString -----> std::string 
我怎样才能做到这一点?
 std::string -----> CString: inline CString toCString(std::string const& str) { return CString(str.c_str()); } 
我对吗?
编辑:
这里有更多的问题:
 我怎样才能将wstring , CString转换为对方? 
 //wstring -> CString, std::wstring src; CString result(src.c_str()); //CString->wstring. CString src; ::std::wstring des(src.GetString()); 
有什么问题吗?
 如何将std::wstring , std::string转换为对方? 
根据CodeGuru :
  CString到std::string : 
 CString cs("Hello"); std::string s((LPCTSTR)cs); 
  但是: std::string不能总是从LPCTSTR构造。 即代码将失败的UNICODE构build。 
 由于std::string只能从LPSTR / LPCSTR构造,使用VC ++ 7.x或更高版本的程序员可以使用转换类(如CT2CA作为中介。 
 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可以从字符或宽字符string构造。 即它可以从char* (即LPSTR )或从wchar_t* ( LPWSTR )转换。 
 换句话说, CStringT (即CStringT )的char-specialization( CStringA , wchar_t -specilization CStringW和TCHAR -pecialization CString可以用char或wide-character来构造,   null终止(null-termination在这里非常重要)  string来源。 
  Althoug IInspectable 在评论中修改了“null-termination”部分: 
NUL终止不是必需的 。
CStringT具有转换构造函数,具有显式长度参数。 这也意味着你可以从embeddedNUL字符的std::string对象构造CStringT对象。
 通过使用std::basic_string<TCHAR>而不是std::string来解决这个问题,不pipe你的字符设置如何,它都可以正常工作。 
 使用指定长度的转换将CString转换为std::string会更有效。 
 CString someStr("Hello how are you"); std::string std(somStr, someStr.GetLength()); 
在紧密的循环中,这会显着提高性能。
 如果你想要更多的C ++  – 就像我使用的那样。 虽然这取决于Boost,但这只是例外。 您可以轻松地删除那些只依赖于STL和WideCharToMultiByte() Win32 API调用。 
 #include <string> #include <vector> #include <cassert> #include <exception> #include <boost/system/system_error.hpp> #include <boost/integer_traits.hpp> /** * Convert a Windows wide string to a UTF-8 (multi-byte) string. */ std::string WideStringToUtf8String(const std::wstring& wide) { if (wide.size() > boost::integer_traits<int>::const_max) throw std::length_error( "Wide string cannot be more than INT_MAX characters long."); if (wide.size() == 0) return ""; // Calculate necessary buffer size int len = ::WideCharToMultiByte( CP_UTF8, 0, wide.c_str(), static_cast<int>(wide.size()), NULL, 0, NULL, NULL); // Perform actual conversion if (len > 0) { std::vector<char> buffer(len); len = ::WideCharToMultiByte( CP_UTF8, 0, wide.c_str(), static_cast<int>(wide.size()), &buffer[0], static_cast<int>(buffer.size()), NULL, NULL); if (len > 0) { assert(len == static_cast<int>(buffer.size())); return std::string(&buffer[0], buffer.size()); } } throw boost::system::system_error( ::GetLastError(), boost::system::system_category); } 
适用于我:
 std::wstring CStringToWString(const CString& s) { std::string s2; s2 = std::string((LPCTSTR)s); return std::wstring(s2.begin(),s2.end()); } CString WStringToCString(std::wstring s) { std::string s2; s2 = std::string(s.begin(),s.end()); return s2.c_str(); } 
这是萨尔的回答,他/她提供了解决scheme:
 CString someStr("Hello how are you"); std::string std(somStr, someStr.GetLength()); 
这在将非典型C-String转换为std :: string时也很有用
对我来说,一个用例是有一个预先分配的字符数组(如Cstring),但它不是NUL终止。 (即SHA摘要)。 上面的语法允许我指定字符数组的SHA摘要的长度,以便std :: string不必查找终止的NUL字符,这可能也可能不在那里。
如:
 unsigned char hashResult[SHA_DIGEST_LENGTH]; auto value = std::string(reinterpret_cast<char*>hashResult, SHA_DIGEST_LENGTH); 
这工作正常:
 //Convert CString to std::string inline std::string to_string(const CString& cst) { return CT2A(cst.GetString()); } 
 所有其他的答案没有完全解决我正在寻找什么是转换CString的飞行,而不是将结果存储在一个variables。 
 解决scheme与上面类似,但是我们需要更多的步骤来实例化一个无名称的对象。 我用一个例子来说明。 这是我需要std::string函数,但我有CString 。 
 void CStringsPlayDlg::writeLog(const std::string &text) { std::string filename = "c:\\test\\test.txt"; std::ofstream log_file(filename.c_str(), std::ios_base::out | std::ios_base::app); log_file << text << std::endl; } 
 如何调用它时,你有一个CString ? 
 std::string firstName = "First"; CString lastName = _T("Last"); writeLog( firstName + ", " + std::string( CT2A( lastName ) ) ); 
 请注意,最后一行不是直接的types转换,但我们正在创build一个无名的std::string对象,并通过其构造函数提供CString 。 
 为什么不从CString到CStringA内部的string构造函数? 
  CString s1("SomeString"); string s2((CStringA)s1); 
 如果你想在其他stringtypes之间轻松转换,可能_bstr_t类更合适? 它支持char , wchar_t和BSTR 。