将Unicode字符写入OStream [英] Writing Unicode Characters to an OStream

查看:76
本文介绍了将Unicode字符写入OStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理Unicode/宽字符,并且正在尝试创建toString方法( Java :: toString equiv ).ostream会处理宽字符吗,如果可以的话,是否有办法警告流的使用者它是unicode出来的?

解决方案

ostream 和C ++的其余部分都不了解Unicode.通常,您使用C ++编写字符串转换,如下所示:

  template< typename Char,typename Traits>std :: basic_ostream< Char,Traits>&运算符<(std :: basic_ostream< Char,Traits>& stream,const YourType& object){返回流<<object.a<<object.b;//管他呢} 

是否获得类似Unicode的内容取决于实现.就Java而言,C ++中的流绝不是文本流,而就Java而言,C ++的字符串也不是字符串.如果您想要一个真正的Unicode字符串,则可能需要看一下 ICU库./p>

I'm working with unicode/wide characters and I'm trying to create a toString method (Java ::toString equiv). Will ostream handle wide characters, if so is there a way to warn the consumer of the stream that it is unicode coming out of it?

解决方案

Neither ostream nor the rest of C++ know anything about Unicode. Usually you write a string conversion in C++ as follows:

template<typename Char, typename Traits>
std::basic_ostream<Char, Traits>&
operator<<(std::basic_ostream<Char, Traits>& stream, const YourType& object) {
    return stream << object.a << object.b;  // or whatever
}

Whether you get something Unicode-like is up to the implementation. Streams in C++ are never text streams in the sense of Java, and C++'s strings are not strings in the sense of Java. If you want a real Unicode string, you might want to have a look at the ICU library.

这篇关于将Unicode字符写入OStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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