【发布时间】:2013-07-26 04:30:05
【问题描述】:
这仅仅是意味着我们对 cout 这样的对象所做的任何事情都会与 stdout 同步(反之亦然?)。这究竟是什么意思。 stdio 是否也与 stdout 同步?
【问题讨论】:
这仅仅是意味着我们对 cout 这样的对象所做的任何事情都会与 stdout 同步(反之亦然?)。这究竟是什么意思。 stdio 是否也与 stdout 同步?
【问题讨论】:
如果同步关闭,C++ 流在某些情况下会更快。
默认情况下,所有标准 C++ 流都与其各自的 C 流同步。
例子:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
cout.sync_with_stdio(false);
cout << "Hello\n";
printf("World\n");
cout << "...\n";
}
输出:
Hello
...
World
将其更改为true 按顺序给出默认结果。
输出:
Hello
World
...
【讨论】:
endl 即使关闭同步也会改变场景
std::cout 和 stdio(如 printf)使用的默认 ostream 是 stdout,但不一定如此。
输出总是可以重定向到另一个目的地。引用这个:http://www.tldp.org/LDP/abs/html/io-redirection.html
【讨论】:
每个 cppreference:
Sets whether the standard C++ streams are synchronized to
the standard C streams after each input/output operation.
【讨论】: