Skip to content

Instantly share code, notes, and snippets.

@cmaspi
Last active January 26, 2024 19:19
Show Gist options
  • Save cmaspi/70939b1c203236b3e52ce2d6765000d3 to your computer and use it in GitHub Desktop.
Save cmaspi/70939b1c203236b3e52ce2d6765000d3 to your computer and use it in GitHub Desktop.
This blog explains the usage of cin.tie, cout.tie, ios_base::sync_with_stdio

WTF is cin.tie(NULL) and ios_base::sync_with_stdio(false)?

Let's go over why it's useful in competitive programming first!

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

These commands are used to make things faster in c++!, but what do we mean by making "things faster"?
These commands only make the input, output faster in C++. Let's understand what each of these commands do.

ios_base:sync_with_stdio(false);

Here's what cppreference has to say about it.

Sets whether the standard C++ streams are synchronized to the standard C streams after each input/output operation. In practice, this means that the synchronized C++ streams are unbuffered, and each I/O operation on a C++ stream is immediately applied to the corresponding C stream's buffer. This makes it possible to freely mix C++ and C I/O. In addition, synchronized C++ streams are guaranteed to be thread-safe (individual characters output from multiple threads may interleave, but no data races occur) If the synchronization is turned off, the C++ standard streams are allowed to buffer their I/O independently, which may be considerably faster in some cases.

By default, the standard I/O streams of C++ are synchronized with the standard I/O streams of C. This makes scanf and printf faster than cin and cout in c++. However, what if you have no intension of using printf and scanf anyways? cin and cout seem easier to use as compared to their counterparts in C.

With the above command, you can disable the synchronization, and allow cin, cout to have their own buffers. This would make cin, cout nearly as fast if not faster than printf and scanf.

#include <iostream>
#include <cstdio>
 
int main()
{
    std::ios::sync_with_stdio(false);
    std::cout << "a\n";
    std::printf("b\n");
    std::cout << "c\n";
}

The output of the above code is

b
a
c

Try it out without sync_with_stdio(false), what is the difference? The output is

a
b
c

Now, try with sync_with_stdio(false) but remove the newline character from printf. What happens now?
The output is

a
c
b

When you have a newline in printf, the previous content in the buffer gets flushed.

cin.tie(NULL);

Let's say you want to ask user to input a certain integer (code below)

#include <iostream>
 
int main()
{
    std::ios::sync_with_stdio(false);
    std::cout << "Enter an integer: ";
    int a;
    std::cin >> a;
}

When you run the code, it will ask you to enter an integer, and then you can enter the integer. Now, add std::cin.tie(NULL); and notice the difference. "Enter an integer: " displays after you already entered the integer. The default behaviour, that is without std::cin.tie(NULL), is that it would ask you for input, it would flush the buffer. That's how it saves time by not flushing the buffer everytime, you can instead manually flush the buffer or it would buffer is also flushed when the program ends, or if the buffer limit reaches which is usually 580 bytes.

Alright! but wth is cout.tie(NULL)

Short answer: Useless thing which a person who doesn't know what they are doing would write.
I should confess, I'm guilty of using it in past because I didn't know what it did. LITTLE LONGER ANSWER
Let's revisit what does it mean for cin to be tied to cout by default? Whenever cin is executed it checks if there's something in buffer, it flushed that before.
But what about cout? it's not tied to anything at least not in most compilers, the reference doesn't explicitly state anything if cout is tied to anything. So, cout.tie(NULL) is useless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment