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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31162367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:52:26  来源:igfitidea点击:

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

c++c

提问by Kshitij Kohli

What is the significance of including

包含的意义是什么

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

in C++ programs?

在 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)?

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

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

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

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

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

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

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

回答by Ionut

The two calls have different meanings that have nothing to do with performance; the fact that it speeds up the execution timeis (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);

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 和 C++ 标准流之间的同步。默认情况下,所有标准流都是同步的,这在实践中允许您混合 C 和 C++ 风格的 I/O 并获得合理和预期的结果。如果禁用同步,则允许 C++ 流拥有自己的独立缓冲区,这使得混合 C 和 C++ 风格的 I/O 成为一种冒险。

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

还要记住,同步的 C++ 流是线程安全的(来自不同线程的输出可能会交错,但不会出现数据竞争)。

cin.tie(NULL);

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

cincout. 绑定流确保在另一个流上的每个 I/O 操作之前自动刷新一个流。

By default cinis tied to coutto ensure a sensible user interaction. For example:

默认情况下cin绑定到cout以确保合理的用户交互。例如:

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

If cinand coutare 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 coutis buffered by default, output is flushed/displayed on the console only on demand or when the buffer is full).

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

So if you untie cinfrom cout, you must make sure to flush coutmanually every time you want to display something before expecting input on cin.

因此,如果您cin从 中解开cout,则必须确保在cout每次想要显示某些内容时手动刷新,然后才能在 上进行输入cin

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

总之,了解他们每个人的作用,了解后果,然后决定您是否真的想要或需要速度提高的可能副作用。

回答by Jean-Baptiste Yunès

This is to synchronize IOs from C and C++ world. If you synchronize, then you have a guaranty that the orders of all IOs is exactly what you expect. In general, the problem is the buffering of IOs that causes the problem, synchronizing let both worlds to share the same buffers. For example cout << "Hello"; printf("World"); cout << "Ciao";; without synchronization you'll never know if you'll get HelloCiaoWorldor HelloWorldCiaoor WorldHelloCiao...

这是为了同步来自 C 和 C++ 世界的 IO。如果您同步,那么您可以保证所有 IO 的顺序正是您所期望的。一般来说,问题是导致问题的 IO 的缓冲,同步让两个世界共享相同的缓冲区。例如cout << "Hello"; printf("World"); cout << "Ciao";; 不同步的,你永远不知道你会得到HelloCiaoWorldHelloWorldCiaoWorldHelloCiao...

tielets you have the guaranty that IOs channels in C++ world are tiedone to each other, which means for example that every output have been flushed before inputs occurs (think about cout << "What's your name ?"; cin >> name;).

tie让你使用保证,在C ++世界的IO通道被捆绑一个彼此,例如这意味着每个输出都输入之前被刷新发生时(考虑cout << "What's your name ?"; cin >> name;)。

You can always mix C or C++ IOs, but if you want some reasonable behavior you must synchronize both worlds. Beware that in general it is not recommended to mix them, if you program in C use C stdio, and if you program in C++ use streams. But you may want to mix existing C libraries into C++ code, and in such a case it is needed to synchronize both.

您总是可以混合使用 C 或 C++ IO,但是如果您想要一些合理的行为,您必须同步这两个世界。请注意,通常不建议混合使用它们,如果您在 C 中编程使用 C stdio,如果您在 C++ 中编程使用流。但是您可能希望将现有的 C 库混合到 C++ 代码中,在这种情况下,需要同步两者。

回答by Don Wakefield

Using ios_base::sync_with_stdio(false);is sufficient to decouple the Cand C++streams. You can find a discussion of this in Standard C++ IOStreams and Locales, by Langer and Kreft. They note that how this works is implementation-defined.

使用ios_base::sync_with_stdio(false);足以解耦CC++流。您可以在Langer 和 Kreft 的Standard C++ IOStreams and Locales 中找到对此的讨论。他们注意到这是如何工作的是实现定义的。

The cin.tie(NULL)call seems to be requesting a decoupling between the activities on cinand cout. I can't explain why using this with the other optimization should cause a crash. As noted, the link you supplied is bad, so no speculation here.

cin.tie(NULL)调用似乎要求在cin和上的活动之间解耦cout。我无法解释为什么将它与其他优化一起使用会导致崩溃。如前所述,您提供的链接不好,所以这里没有任何猜测。