ios_base::sync_with_stdio(false) 的意义;cin.tie(NULL);

Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意义;cin.tie(NULL);)
本文介绍了ios_base::sync_with_stdio(false) 的意义;cin.tie(NULL);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

包含的意义是什么

ios_base::sync_with_stdio(false);
cin.tie(NULL);

在 C++ 程序中?

在我的测试中,它加快了执行时间,但是否有我应该担心的测试用例包含这个?

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

这两个语句是否总是必须在一起,还是第一个就足够了,即忽略cin.tie(NULL)?

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

此外,如果其值已设置为 false,是否允许同时使用 C 和 C++ 命令?

Also, is it permissible to use simultaneous C and C++ commands if its value has been set to false?

https://www.codechef.com/viewsolution/7316085

以上代码运行良好,直到我在 C++ 程序中使用 scanf/printf 并将值设为 true.在这种情况下,它给出了分段错误.对此有什么可能的解释?

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 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);

这将 cincout 分开.绑定流可确保在对另一个流进行每次 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;

如果 cincout 是绑定的,你可以期待在程序提示用户输入之前输出被刷新(即,在控制台上可见).如果您解开流,程序可能会阻止等待用户输入他们的姓名,但输入姓名"消息尚不可见(因为 cout 在默认情况下被缓冲,输出被刷新/显示在控制台仅在需要时或缓冲区已满时).

If cin and cout are tied, you can expect the output to be flushed (i.e., visible on the console) before the program prompts input from the user. If you untie the streams, the program might block waiting for the user to enter their 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).

因此,如果您将 cincout 解开,则必须确保每次要在等待输入之前显示内容时手动刷新 coutcin 上.

So if you untie cin from cout, you must make sure to flush cout manually every time you want to display something 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_base::sync_with_stdio(false) 的意义;cin.tie(NULL);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(将 RGB 转换为 HSV 并将 HSV 转换为 RGB 的算法,范围为 0-255)
How to convert an enum type variable to a string?(如何将枚举类型变量转换为字符串?)
When to use inline function and when not to use it?(什么时候使用内联函数,什么时候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Is TCHAR still relevant?(TCHAR 仍然相关吗?)
C99 stdint.h header and MS Visual Studio(C99 stdint.h 头文件和 MS Visual Studio)