-2

Can anyone explain the need of this statement and change in outcome if we don't use it. I read this: Significance of ios_base::sync_with_stdio(false); cin.tie(NULL); but was unable to understand.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 3
    There's not much point in us re-answering this question if we don't know which parts you didn't get. You'll just... not get it again, and we'll have replicated all the information for nothing. Instead, you should ask for clarification on the existing question. – Lightness Races in Orbit Jan 08 '20 at 18:44

1 Answers1

2

Usually input and output using the standard C++ streams are synchronized with the C standard I/O streams. For example output through std::cout is synchronized with stdout, and input with std::cin is synchronized with stdin.

This synchronization can slow down output and input with std::cout and std::cin (respectively), so if a lot of output is written or lot of input is read this synchronization can can be disabled, by calling sync_with_stdio(false).


The other issue about tie is that std::cout and std::cin are in a way "tied" to each other by default. That means if you attempt to read input from std::cin then all buffered output in the std::cout stream buffer will be flushed.

For interactive programs this is good, as that ensures that possible prompts for input will be written before actually reading input. But again, this could incur some performance penalties in the flushing of the output if there's lot of bulk input being done.


Please note that for normal programs these possible performance penalties are small, almost negligible, and won't matter much. But it's a pattern to disable it for so-called "competitive" programming.

My recommendation is to not use it at all, unless you actually have measured, benchmarked and profiled that such problems are among the top-two or possibly top-three bottlenecks in your program, and that there's a strict performance requirement that disabling these things will help with.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621