关闭cin与Cscanf同步的弊端
我最近从这个关于cinC++ 的问题中学到了一些东西,它的速度与 C 中的速度进行了比较scanf。调用cin对象比scanf平常调用函数慢得多,但是当我阅读接受的答案时,我明白如果我们调用std::ios::sync_with_stdio(false);,cin同步scanf是关闭,cin速度变得比scanf这种情况下更快甚至更快。
那么,如果我们关闭它,我们会面临一些问题吗?如果是这样,有什么问题,在C中关闭cin同步是否好scanf?谢谢你的帮助。
回答
如果您在同一个流上使用两组 I/O 函数(标头<cstdio>或<stdio.h>和也<iostream>)(例如,stdin流与两个scanf和都关联cin),那么您最好让它们保持同步。
如果任何一个数据流只使用一个I / O系列可以关闭同步(你仍然可以使用fscanf一个特定的文件,并cin与stdin例如,只要单独的流都参与)。
- @NathanPierson: Backward compatibility is a bigger goal than fast-by-default. It seems more worthwhile to share buffers in an efficient manner where the synchronization cost goes away, than it does to try to disable synchronization by default.