我在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;
wstring到BSTR
CComBSTR tstr = path.c_str();  
if(trk->set_Info(tstr) == S_OK)
    return true;
字符串资源到wstring
CString t;
wstring url;
t.LoadString(IDS_SCRIPTURL);
url = t;
GetProfileString()返回一个CString.
整数到字符串格式:
wchar_t total[32];
swprintf_s(total, 32, L"%d", trk->getInt());
wstring tot(total);
我输入的字符串只包含数字(只是简单的拉丁文字符串,0-9,所以例如"0123"),存储为std :: wstring,我需要每个字符串作为char*.这对我来说最好的方法是什么?这是我最初的做法:
void type::convertWStringToCharPtr(_In_ std::wstring input, _Out_ char * outputString)
{
    outputString = new char[outputSize];
    size_t charsConverted = 0;
    const wchar_t * inputW = input.c_str();
    wcstombs_s(&charsConverted, outputString, sizeof(outputString), inputW, input.length());
}
编辑:下面的代码工作.谢谢大家!
void type::convertWStringToCharPtr(_In_ std::wstring input, _Out_ char * outputString)
{
    size_t outputSize = input.length() + 1; // +1 for null terminator
    outputString = new char[outputSize];
    size_t charsConverted = 0;
    const wchar_t * inputW = input.c_str();
    wcstombs_s(&charsConverted, outputString, outputSize, inputW, input.length());
}
我目前正在开发一个跨平台的C++库,我打算用Unicode识别它.我目前通过typedef和宏对std :: string或std :: wstring进行编译时支持.这种方法的缺点是它会强制您使用宏,L("string")并根据字符类型大量使用模板.
支持std :: wstring只支持和反对的论据是什么?
使用std :: wstring是否会影响GNU/Linux用户群,首选UTF-8编码?
我有一个奇怪的问题,我用
wifstream a("a.txt");
wstring line;
while (a.good()) //!a.eof()  not helping
{
     getline (a,line);
      //...
     wcout<<line<<endl;
}
和它的作品很好地为这样的txt文件 http://www.speedyshare.com/files/29833132/a.txt (抱歉的联系,但它仅仅是80个字节,所以它不应该是一个问题,得到它,如果SO换行上的ic/p丢失了)但是当我添加例如水(来自http://en.wikipedia.org/wiki/UTF-16/UCS-2#Examples)到任何加载线的行时停止.我错误的认为getline将wstring作为一个输入和wifstream,因为其他人可以咀嚼任何txt输入...有没有办法读取文件中的每一行,即使它包含时髦的字符?
以下代码在Visual Studio 2010中编译,但无法在Visual Studio 2012 RC中编译.
#include <string>
// Windows stuffs
typedef __nullterminated const wchar_t *LPCWSTR;
class CTestObj {
public:
    CTestObj() {m_tmp = L"default";};
    operator LPCWSTR()  { return m_tmp.c_str(); }       // returns const wchar_t*
    operator std::wstring() const { return m_tmp; }     // returns std::wstring
protected:
    std::wstring m_tmp;
};
int _tmain(int argc, _TCHAR* argv[])
{
    CTestObj x;
    std::wstring strval = (std::wstring) x;
    return 0;
}
返回的错误是:
错误C2440:'type cast':无法转换
'CTestObj'为'std::wstring'
No构造函数可以采用源类型,或构造函数重载解析不明确
我已经意识到,注释掉任何转换运算符都会修复编译问题.我只想了解:
c++ visual-studio-2010 wstring visual-c++ visual-studio-2012
我有一个简单的字符串 std::wstring 
std::wstring tempStr = _T("F:\\Projects\\Current_??_\\Cam.xml");
我想把这个字符串存储在一个std::string.
我尝试了下面的代码,但结果与输入字符串不同
std::wstring tempStr = _T("F:\\Projects\\Current_??_\\Cam.xml");
//setup converter
typedef  std::codecvt_utf8_utf16 <wchar_t> convert_type;
std::wstring_convert<convert_type, wchar_t> converter;
//use converter (.to_bytes: wstr->str, .from_bytes: str->wstr)
std::string converted_str = converter.to_bytes( tempStr );
输入字符串中存在的韩语字符串将转换为"ìžë™".
有什么办法可以在std :: string中获得相同的字符串吗?
预期结果:
converted_str应该包含F:\ Projects\Current_자동_\Cam.xml
下面是调试的屏幕截图,显示3个场景中的3个值(以3种方式转换).但它们都没有给出所需的价值.

在C++中是否有可移植的wchar_t?在Windows上,它的2个字节.其他一切都是4个字节.我想在我的应用程序中使用wstring,但如果我决定将其移植到端口,这将导致问题.
我有一个广泛使用 wstring 的库。我需要使用 NSLog 输出更改和外部数据是否有一种简单的方法(不太昂贵)来使用中间函数输出 wstring。使用 va_list 将每个 wstring 转换为 NSString 是我现在能想到的唯一方法。
编辑:更精确。我有一个多平台库。我添加了一个日志记录宏 MYLog。
编辑 我必须从 C++ 调用我的 MYLog,但此时我无法访问 Objective-C。所以问题是在调用 MYLog 之前我无法转换 std::wstring。
通过 MYLog 我希望能够使用 NSLog 或中间体,如下所示:
MYLog("Received %ls(%d) from user %ls %ls cp: %ls /nRAW:/t%ls",
       &d.name, d.id, &d.user.firstName, &d.user.lastName,
       &d.caption, &d.rawText);
在这里(最初来自这里)我发现了 NSString 的这个很好的补充:
@interface NSString (cppstring_additions)
+(NSString*) stringWithwstring:(const std::wstring&)string;
+(NSString*) stringWithstring:(const std::string&)string;
-(std::wstring) getwstring;
-(std::string) getstring;
@end
@implementation NSString (cppstring_additions)
#if TARGET_RT_BIG_ENDIAN
const NSStringEncoding kEncoding_wchar_t = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF32BE);
#else
const NSStringEncoding kEncoding_wchar_t = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF32LE); …我目前正在 Windows 上进行开发,但我想让我的应用程序稍后跨平台。它没有/不需要 GUI。
我一直在使用 wstrings,希望这是最好的解决方案。我在项目设置中使用“多字节字符集”。
所有其他平台也支持 wstring 吗?
我满足了在控制台中对齐输出文本的要求。输出文本存储为std::wstring并编码为UTF-8. 使此任务变得棘手的是,输出文本同时包含 ASCII 字符和日语字符,例如\xe3\x83\x8a5\xe5\x9b\x9e1\xe3\x82\xb7\xe3\x83\xa7\xe3\x83\xb34a\xe3\x82\xb9F。由于ASCII字符和日文字符的宽度不同,使用setw()或直接无法对齐。\n例如,wprintf(L"%-10s")
#include <iostream>\n#include <iomanip>\nusing namespace std;\nint main(){\n    std::locale::global(std::locale(""));\n    wstring s[] = {L"\xe7\x9f\xad3\xe3\x83\x9e231\xe3\x83\xbc\xe2\x97\x8b",L"\xe3\x81\xae\xe3\x81\x8d3\xe3\x83\xbc\xe3\x83\x8a",L"\xe3\x81\x90\xe3\x83\x9e",L"\xe3\x82\x8d\xe3\x81\xab\xe3\x83\x88"};\n    for(int i=0;i<4;i++)    wcout << setw(10) << s[i] <<123<< endl;\n}\n会像:
\n    \xe7\x9f\xad3\xe3\x83\x9e231\xe3\x83\xbc\xe2\x97\x8b123\n     \xe3\x81\xae\xe3\x81\x8d3\xe3\x83\xbc\xe3\x83\x8a123\n       \xe3\x81\x90\xe3\x83\x9e123\n      \xe3\x82\x8d\xe3\x81\xab\xe3\x83\x88123\n但如果文本仅包含 ASCII 字符,则它可以正常工作。
\n我知道我可以自己编写一个新函数来对齐它,但我想知道是否已经有一个可靠的解决方案。
\n