C++0x 中的 Unicode 支持

Unicode support in C++0x(C++0x 中的 Unicode 支持)
本文介绍了C++0x 中的 Unicode 支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 C++0x 中使用新的 unicode 字符.所以我写了示例代码:

#include #include <字符串>int main(){std::u32string str = U"Hello World";std::basic_ofstreamfout("输出.txt");fout<

但是在执行这个程序后,我得到了空的 output.txt 文件.那么为什么不打印 Hello World?

还有已经为这些类型定义的coutcin,或者stdinstdout不支持 Unicode?

我使用的是 g++ 和 Linux.

АТТЕNTION.我发现,标准委员会驳回了 C++0x 中的 Unicode 流.所以以前接受的答案不再正确.有关详细信息,请参阅我的回答!

解决方案

Unicode 字符串文字支持 开始 在 GCC 4.5 中.也许这就是问题所在.

经过一些挖掘,我发现这个新的 unicode 文字的流在 N2035 它是 包括在标准草案中.根据此文档,您需要 u32ofstream 来输出字符串,但 GCC 4.5 C++0x 库中不存在此类.

作为一种解决方法,您可以使用普通的 fstream:

std::ofstream fout2("output2.txt", std::ios::out | std::ios::binary);fout2.write((const char *)str.c_str(), str.size() * 4);

这样我就在我的 Intel 机器上以 UTF-32LE 格式输出你的字符串(小端).

我对 u32ofstream 的状态有点错误:根据 最新草案关于 C++ 标准委员会的网站 你必须像你一样使用 std::basic_ofstream.此类将使用必须在标准库中实现的 codecvt<char32_t,char,typename traits::state_type> 类(参见 §27.9.1.1 末尾)(搜索 codecvt<char32_t 在文档中),但它在 GCC 4.5 中不可用.

I'm trying to use new unicode characters in C++0x. So I wrote sample code:

#include <fstream>
#include <string>
int main()
{
    std::u32string str = U"Hello World";

    std::basic_ofstream<char32_t> fout("output.txt");

    fout<<str;  
    return 0;
}

But after executing this program I'm getting empty output.txt file. So why it's not printing Hello World?

Also is there something like a cout and cin already defined for these types, or stdin and stdout doesn't support Unicode?

Edit: I'm using g++ and Linux.

EDIT:АТТЕNTION. I have discovered, that standard committee dismissed Unicode streams from C++0x. So previously accepted answer is not correct anymore. For more information see my answer!

解决方案

Unicode string literals support began in GCC 4.5. Maybe that's the problem.

[edit]

After some digging I've found that streams for this new unicode literals are described in N2035 and it was included in a draft of the standard. According to this document you need u32ofstream to output you string but this class is absent in GCC 4.5 C++0x library.

As a workaround you can use ordinary fstream:

std::ofstream fout2("output2.txt", std::ios::out | std::ios::binary);
fout2.write((const char *)str.c_str(), str.size() * 4);

This way I've output your string in UTF-32LE on my Intel machine (which is little-endian).

[edit]

I was a little bit wrong about the status of u32ofstream: according to the latest draft on the The C++ Standards Committee's web site you have to use std::basic_ofstream<char32_t> as you did. This class would use codecvt<char32_t,char,typename traits::state_type> class (see end of §27.9.1.1) which has to be implemented in the standard library (search codecvt<char32_t in the document), but it's not available in GCC 4.5.

这篇关于C++0x 中的 Unicode 支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)