众所周知,cout 在 VS2010 中是无缓冲的(参见 Stephan Lavavej 的帖子)here http://connect.microsoft.com/VisualStudio/feedback/details/642876/std-wcout-is-ten-times-slower-than-wprintf-performance-bug-in-c-library)。为什么在下面的代码中我可以使用 cout 的缓冲区构造 ostream hexout ?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
// Construct ostream hexout with cout's buffer and setup hexout to print hexadecimal numbers
ostream hexout(cout.rdbuf());
hexout.setf (ios::hex, ios::basefield);
hexout.setf (ios::showbase);
// Switch between decimal and hexadecimal output using the common buffer
hexout << "hexout: " << 32 << " ";
cout << "cout: " << 32 << " ";
hexout << "hexout: " << -1 << " " ;
cout << "cout: " << -1 << " ";
hexout << endl;
}
每个流都有一个与其关联的 basic_streambuf。 “无缓冲”只是意味着 basic_streambuf 不维护内部缓冲区(一块内存)来缓冲输入/输出,而是直接从文件(或控制台等)读取/写入。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)