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

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

问题描述

我使用unicode / wide字符,我正在尝试创建一个toString方法( Java :: toString equiv )。如果有,是否有一种方法来警告流的消费者它是unicode从它出来?

解决方案

ostream 或其他的C ++都不懂Unicode。通常你在C ++中写一个字符串转换如下:

  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; //或任何
}

。 C ++中的流不是Java意义上的文本流,C ++的字符串不是Java意义上的字符串。如果您想要一个真正的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天全站免登陆