C++ STD :: String到STD :: WSTRING -Windows vs Linux的转换问题
将字符串从std :: string转换为std :: string :: Wstring
我正在尝试使用 std :: Wstring wstmp(str.begin(),str.end()) ;
在Windows上起作用,但是在Linux上它返回“ POK \ XFFFFFC3 \ XFFFFFFFA9MON”
如何使其在Linux上工作?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这在Posix上对我有用。
wstring
在末尾保持正确的字符串。@nathanoliver的重要说明:
std :: codecvt_utf8_utf16
在C ++ 17中弃用,并可以从未来版本的标准中删除。This worked for me on POSIX.
The
wstring
holds the correct string at the end.Important note by @NathanOliver:
std::codecvt_utf8_utf16
was deprecated in C++17 and may be removed from the standard in a future version.您似乎在这里遇到的问题是,它正在将
é
的两个代码单元视为转换时的单独代码点。使用C ++ 17的标准库是没有好的方法,因为std :: Wstring_convert
被弃用而未得到适当的替换。您有几个选项,它们都不是很棒:std :: wstring_convert
,而忽略了弃用警告,并且在将来的C ++修订版中可以将其删除。同样有些无关,但是如果您关心跨不同平台的一致性,则应使用
std :: U16String
或std :: U32String
。std :: Wstring
的字符大小取决于wchar_t
的大小,该大小在不同的编译器和平台之间变化。The problem you seem to be running into here is that it's treating the
é
's two code units as separate code points when converting. There's no good way to do this with the standard library past C++17, asstd::wstring_convert
was deprecated without being given a proper replacement. You have several options, none of them great:std::wstring_convert
and ignore the deprecation warnings and the fact that it may be removed in a future revision of C++.Also somewhat unrelated, but if you care about consistency across different platforms you should be using
std::u16string
orstd::u32string
.std::wstring
's character size depends on the size ofwchar_t
, which varies between different compilers and platforms.