的ios_bas​​e :: sync_with_stdio(假)的意义; cin.tie(NULL); [英] Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);

查看:262
本文介绍了的ios_bas​​e :: sync_with_stdio(假)的意义; cin.tie(NULL);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是包括的ios_bas​​e :: sync_with_stdio(假)的意义; cin.tie(NULL); 的C ++程序?

What is the significance of including ios_base::sync_with_stdio(false); cin.tie(NULL); in C++ Programs?

明显看出,加快执行时间,但有一个测试情况下,我应该担心由包括本?

As evident, it speeds up the execution time, but is there a test case I should be worried about by including this?

在做陈述2总是在一起,或者是第一个充分的,也就是忽略 cin.tie(NULL)

Do the 2 statements always have to be together, or is the first one sufficient, ie ignoring cin.tie(NULL)

另外,是不是允许使用同步 C C ++,如果它的价值已经被设置为False 命令?

Also, is it not permissible to use simultaneous C and C++ commands if it's value has been set to False?

<一个href=\"http://www.$c$cchef.com/submit/complete/639410-8514--5593e450e9078\">http://www.$c$cchef.com/submit/complete/639410-8514--5593e450e9078

以上code工作得很好,直到我用 scanf函数/ printf的 C ++ 程序与值作为真。在这种情况下,它给了一个分段错误。有什么能为这个可能的解释?

The above code worked fine, until I used scanf/printf in a C++ program with the value as True. In this case, it gave a segmentation fault. What could be the possible explanation for this?

推荐答案

这两个调用不同的含义,在核心无关与性能,即事实上,它加快了执行时间的是(或可能是的)只是一个副作用。您应该了解他们每个人也并没有一味地将它们包括在每一个程序,因为它们看起来像一个最优化。

The two calls have different meanings that at the core have nothing to do with performance, the fact that it speeds up the execution time is (or might be) just a side effect. You should understand what each of them does and not blindly include them in every program because they look like an optimization.

ios_base::sync_with_stdio(false);

这禁用C和C ++标准流之间的同步。默认情况下所有的标准流是同步的,这在实践中允许你混合C和C ++风格的I / O,并得到合理的和预期的结果。如果禁用同步,然后C ++流都不允许有自己独立的缓冲区,这使得混合C和C ++风格I / O的冒险。

This disables the synchronization between the C and C++ standard streams. By default all standard streams are synchronized, which in practice allows you to mix C and C++ style I/O and get sensible and expected results. If you disable the synchronization then C++ streams are allowed to have their own independent buffers, which makes mixing C and C++ style I/O an adventure.

另外请记住,同步C ++流是线程安全的(来自不同线程的输出可以交错,但你没有数据的比赛)。

Also keep in mind that synchronized C++ streams are thread-safe (output from different threads may interleave, but you get no data races).

cin.tie(NULL);

这解开 CIN COUT 。绑流保证一个流的其他数据流的每个I / O操作之前自动刷新。

This unties cin from cout. Tied streams ensure that one stream is flushed automatically before each I/O operation on the other stream.

在默认情况下 CIN 绑定到 COUT 来确保合理的用户交互。例如:

By default cin is tied to cout to ensure a sensible user interaction. For example:

std::cout << "Enter name:";
std::cin >> name;

如果 CIN COUT 绑,你是确保输出刷新(例如,在可见程序前控制台)从用户希望输入。如果你解开流,该程序可能会阻止等待用户输入他的名字,但输入名称消息尚未出现(因为 COUT 默认情况下缓冲,输出刷新/只在需要时或显示在控制台上,当缓冲区已满)。

If cin and cout are tied, you're sure that the output is flushed (e.g. visible on the console) before the program expects input from the user. If you untie the streams, the program might block waiting for the user to enter his name but the "Enter name" message is not yet visible (because cout is buffered by default, output is flushed/displayed on the console only on demand or when the buffer is full).

所以,如果你解开 CIN COUT 你必须确保手动刷新 COUT 期待输入CIN 之前,真正显示你想要的东西,每次。

So if you untie cin from cout you must make sure to manually flush cout every time you want something to really be displayed before expecting input on cin.

总之,知道他们每个人确实了解其后果,然后再决定,如果你真的想要或需要的可能的速度提高副作用。

In conclusion, know what each of them does, understand the consequences and then decide if you really want or need the possible side effect of speed improvement.

这篇关于的ios_bas​​e :: sync_with_stdio(假)的意义; cin.tie(NULL);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆