gpt4 book ai didi

c++ - 在 cout.rdbuf(fout.rdbuf()) 之后哪个语言环境将应用于 cout?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:52:00 24 4
gpt4 key购买 nike

#include <locale>
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
ofstream fout("test.txt");

fout.imbue(locale("chs"));
cout.imbue(locale("C"));

cout.rdbuf(fout.rdbuf());
cout << "中文"; // Which locale will apply to here? "C" or "chs"?
}

问题在代码中有注释。

最佳答案

来自 http://www.cplusplus.com/reference/ios/ios/imbue/

std::ios::imbue <ios>

locale imbue ( const locale& loc );

Imbue locale

Associates loc to both the stream and its associated stream buffer (if any) as the new locale object to be used with locale-sensitive operations.

All callback functions registered with register_callback with imbue_event as its first parameter are called.

In fact, this member function calls its inherited homonym ios_base::imbue(loc), and if the stream is associated with a stream buffer, also calls rdbuf()->pubimbue(loc).

另请参阅 http://stdcxx.apache.org/doc/stdlibug/27-4.html

27.4.4 Collaboration of Locales and Iostreams

The base class ios_base contains a locale object. The formatting and parsing functions defined by the derived stream classes use the numeric facets of that locale.

The class template basic_ios holds a pointer to the stream buffer. This stream buffer has a locale object, too, usually a copy of the same locale object used by the functions of the stream classes. The stream buffer's input and output functions use the code conversion facet of the attached locale.

在您的情况下,它将使用“C”数字区域设置和“chs”字符区域设置。

关于c++ - 在 cout.rdbuf(fout.rdbuf()) 之后哪个语言环境将应用于 cout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14719330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com