在C++中使用字符串最喜欢哪种方式?一个C风格的chars?还是wchar_t?CString,std::basic_string,std::string,BSTR或CComBSTR?
当然,每个都有自己的应用领域,但无论如何,这是你最喜欢的,为什么?
我在C++中有一个使用std :: string和std :: wstring的MFC应用程序,并经常从一个转换为另一个,还有很多其他的废话.我需要将所有内容标准化为单一格式,所以我想知道是否应该使用CString或std :: wstring.
在应用程序中,我需要从字符串表生成字符串,处理大量需要常量tchar或wchar_t指针的Windows调用,编辑控件,并与需要BSTR的COM对象API进行交互.
我也有字符串向量,所以CStrings向量有什么问题吗?
哪一个更好?各自的优点和缺点是什么?
例子
BSTR到wstring
CComBSTR tstr;
wstring album;
if( (trk->get_Info((BSTR *)&tstr)) == S_OK && tstr!= NULL)
album = (wstring)tstr;
Run Code Online (Sandbox Code Playgroud)
wstring到BSTR
CComBSTR tstr = path.c_str();
if(trk->set_Info(tstr) == S_OK)
return true;
Run Code Online (Sandbox Code Playgroud)
字符串资源到wstring
CString t;
wstring url;
t.LoadString(IDS_SCRIPTURL);
url = t;
Run Code Online (Sandbox Code Playgroud)
GetProfileString()返回一个CString.
整数到字符串格式:
wchar_t total[32];
swprintf_s(total, 32, L"%d", trk->getInt());
wstring tot(total);
Run Code Online (Sandbox Code Playgroud)