简体   繁体   English

在C ++中灌输的目的是什么?

[英]What the purpose of imbue in C++?

I'm working with some code today, and I saw: 我今天正在使用一些代码,我看到:

extern std::locale g_classicLocale;
class StringStream : public virtual std::ostringstream
{
 public:
        StringStream() { imbue(g_classicLocale); }
        virtual ~StringStream() {};
};

Then I came in face of imbue . 然后我面对imbue What is the purpose of the imbue function in C++? C ++中的imbue函数的目的是什么? What does it do? 它有什么作用? Are there any potential problems in using imbue (non-thread safe, memory allocation)? 使用imbue (非线程安全,内存分配)是否存在任何潜在问题?

imbue is inherited by std::ostringstream from std::ios_base and it sets the locale of the stream to the specified locale. imbuestd::ios_basestd::ostringstream继承,它将流的语言环境设置为指定的语言环境。

This affects the way the stream prints (and reads) certain things; 这会影响流打印(和读取)某些事物的方式; for instance, setting a French locale will cause the decimal point . 例如,设置法语区域设置将导致小数点. to be replaced by , . 到被替换,

C++ streams perform their conversions to and from (numeric) types according to a locale , which is an object that summarizes all the localization information needed (decimal separator, date format, ...). C ++流根据locale执行与(数字)类型的转换, locale是汇总所需的所有本地化信息的对象(小数分隔符,日期格式......)。

The default for streams is to use the current global locale, but you can set to a stream a custom locale using the imbue function, which is what your code does here - I suppose it's setting the default C locale to produce current locale-independent text (this is useful eg for serialization purposes). 流的默认设置是使用当前的全局语言环境,但您可以使用imbue函数将自定义语言环境设置为流,这是您的代码在此处执行的操作 - 我认为它设置默认的C语言环境以生成当前与语言环境无关的文本(这对于序列化目的很有用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM