Unicode支持C ++ 0x [英] Unicode support in C++0x

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

问题描述

我想在C ++ 0x中使用新的unicode字符。
所以我写了示例代码:

  #include< fstream& 
#include< string>
int main()
{
std :: u32string str = UHello World;

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

fout<< str;
return 0;
}

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



也有一些像 cout 或者 stdin stdout 不支持Unicode?



编辑:我使用g ++和Linux。



编辑:АТТЕNTION。我发现,标准委员会驳回了C ++ 0x的Unicode流。所以以前接受的答案不再正确。有关详情,请参阅我的回答

解决方案

Unicode字符串字面支持在GCC 4.5开始



在一些挖掘之后,我发现, ve发现这个新的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);

这样,我输出你的字符串在UTF-32LE在我的英特尔机器endian)。



[edit]



u32ofstream 的状态:根据最新草稿 C ++标准委员会的网站,您必须使用 std :: basic_ofstream< char32_t> 。这个类将使用标准库中实现的 codecvt 类(见§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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆