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);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!




在 C++ 程序中?


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


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?

以上代码运行良好,直到我在 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.


这会禁用 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).


这将 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)