C++ Competitive Programming : Fast Input/Output (Fast I/O)

Hello coderz!! Welcome to CodingBroz where you learn new tips and tricks about Competitive Programming in C++ and many more things related to programming.

Today in this post, I am going to tell you about an awesome method which you can use to optimize the Input and Output method in C++ for competitive Programming.

C++ Competitive Programming : Fast Input/Output

This method in C++ will help you reduce your Input and Output time and make it more optimized so you can save time during any competitive programming contest.

It is a method which is widely used across the globe by most of the C++ Competitive programmers. This method is known as Fast I/O or Fast Input/Output technique.

What is Fast I/O – C++ Competitive Programming

As we know C++ is a backward compatible language which means it supports most of the C programming language syntax.

In C programming, we use printf() and scanf() function for output and input respectively.

In C++, we can take input and output using both scanf() & cin and printf() & cout respectively.

 It is recommended by many to use scanf() and printf() instead of cin and cout to achieve fast Input and output process, but we can achieve the same using cin and cout too using these 2 lines inside the main function :

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

In C++ there is synchronisation between the two languages and their I/O function i.e. between cin/cout and scanf()/printf(). 

Now, using this syntax :  ios_base::sync_with_stdio(false);

We can turn off the synchronisation between C and C++ standard streams. We are doing this to avoid unnecessary overriding.

Now, again there is a synchronisation between cin and cout in C++.

By using the second line mentioned above i.e.  cin.tie(NULL) , we can untie the cin and cout stream.

This tie between cin and cout means, when we are using cin, cout needs to be flushed for giving the output and when we are providing the output cin needs to be flushed to take other input.

This synchronisation is helpful in interactive problems where we need cin and cout again and again, but is not helpful in competitive programming as it slows down the process for large I/O operations.

So, to untie the cin and cout stream in c++ we use this code : cin.tie(NULL);

Extra Tip #1 :

You can include all the Standard Template Library (STL) functions available by using one single include :

#include <bits/stdc++.h>

Here, is your complete template for Competitive Programming in C++ with fast I/O which you should use in Competitive contests and getting some extra edge :

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}

Extra Tip #2:

It is also recommended that you should use cout << “\n” to change the line instead of using cout << endl, because endl unnecessarily slows down the process by calling the flush stream. 

Broz Who Code

CodingBroz

Leave a Comment

Your email address will not be published. Required fields are marked *