[英]Is the C++11 <codecvt> header available in the latest GCC?
After reading Is <codecvt>
not a standard header? 阅读后
<codecvt>
不是标准标题? I am not sure what to do as my Windows version of the codebase uses <codecvt>
to convert between wide strings and strings. 我不知道该怎么办,因为我的Windows版本的代码库使用
<codecvt>
来在宽字符串和字符串之间进行转换。 I currently use GCC 4.7 for Linux version of my code. 我目前使用GCC 4.7 for Linux版本的代码。 Is
<codecvt>
also missing in the latest GCC? 最新的GCC中是否还缺少
<codecvt>
? What would be a workaround? 什么是解决方法?
BTW, as it's stated here the following code wouldn't work with GCC: 顺便说一下 ,正如这里所说,以下代码不适用于GCC:
wstring ws = L"hello";
string ns(ws.begin(), ws.end());
How about using mbsrtowcs
and wcsrtombs
? 使用
mbsrtowcs
和wcsrtombs
怎么wcsrtombs
? Though they come from C are not very convenient to use with std::string
and std::wstring
(but you can always make your own convenient C++ functions based on them). 尽管它们来自C并不是很方便与
std::string
和std::wstring
(但你总是可以根据它们制作自己方便的C ++函数)。 Besides, the conversion is performed based on the currently installed C locale. 此外,转换是基于当前安装的C语言环境执行的。 So, your example code snippet can be changed to something like:
因此,您的示例代码段可以更改为:
std::mbstate_t state = {};
const wchar_t* p = L"hello";
std::string ns(std::wcsrtombs(NULL, &p, 0, &state) + 1);
std::wcsrtombs(&ns[0], &p, ns.size(), &state);
ns.pop_back();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.