C++ Convert string (or char*) to wstring (or wchar_t*)

后端 未结 16 2179
难免孤独
难免孤独 2020-11-22 05:57
string s = \"おはよう\";
wstring ws = FUNCTION(s, ws);

How would i assign the contents of s to ws?

Searched google and used some techniques but

16条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 06:44

    Your question is underspecified. Strictly, that example is a syntax error. However, std::mbstowcs is probably what you're looking for.

    It is a C-library function and operates on buffers, but here's an easy-to-use idiom, courtesy of TBohne (formerly Mooing Duck):

    std::wstring ws(s.size(), L' '); // Overestimate number of code points.
    ws.resize(std::mbstowcs(&ws[0], s.c_str(), s.size())); // Shrink to fit.
    

提交回复
热议问题