I saw many people using this line before their code! ios_base :: sync_with_stdio(false), cin.tie(NULL), cout.tie(0) I think it is for fast input /output.But how it works?
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
I saw many people using this line before their code! ios_base :: sync_with_stdio(false), cin.tie(NULL), cout.tie(0) I think it is for fast input /output.But how it works?
Name |
---|
The C++ reference website has some details about this issue in the following pages.
ios_base::sync_with_stdio
basic_ios::tie
The command may be coded as
ios_base::sync_with_stdio(false), cin.tie(nullptr);
as the C++ standard output streamcout
should not be tied to any other output stream by default.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.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);
This untiescin
fromcout
. Tied streams ensure that one stream is flushed automatically before each I/O operation on the other stream.By default cin is tied to cout to ensure a sensible user interaction. For example:
If
cin
andcout
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 (becausecout
is buffered by default, output is flushed/displayed on the console only on demand or when the buffer is full).So if you untie
cin
fromcout
, you must make sure to flushcout
manually every time you want to display something before expecting input oncin
.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.
credits : stack OverFlow @TriskalJM Here For Orignal Q&A Thanks to : @EnhanceTheVoidMainer
Remember to credit your source of information when you hit copy-paste!