Fast Input and Output in C++
In competitive programming, it's critical to read input as quickly as possible in order to save time. "Warning: Big I / O data, be aware of certain languages (but most should be correct if the operating system is sufficiently developed)," you have probably read in many of the problem statements. Faster I / O systems are key to solving such problems.
For faster and more intuitive input, scanf / printf is often recommended over cin / cout. However, by setting the following two lines in your main (), you can still use cin / cout and get the same functionality as scanf / printf:
ios_base :: sync_with_stdio ( false ) ;
When called before the system performs its first input or output action, it turns on or off the synchronization of all normal C ++ streams and your normal C-stream. This sync can be avoided by adding ios base :: sync with stdio ( false ); (which is automatically true) before every I / O action. Stable member std :: ios base function.
cin.tie ( NULL ) ;
tie () is a function that merely ensures that std :: cout is flushed before std :: cin accepts a command. This is handy for interactive console applications that require the console to be updated on a regular basis, but it slows down the program when dealing with big amounts of data. The NULL portion just returns a reference to NULL.
Furthermore, the standard template library (STL) may be included with a single include:
#include < bits/stdc++.h >
As an example, your competitive programming template may look like this:
#include < bits/stdc++.h >
using namespace std ;
int main ( )
{
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
return 0 ;
}
It is preferable to use cout "n"; rather than cout endl;. endl is more time consuming since it requires a flushing stream, which is frequently unneeded. (If you are upgrading, for example, a collaborative progress bar, you will need to delete it, but not if you are writing a million data lines.) Instead of 'house, write' n. Problem INTEST - Enormous Input Teston SPOJ, we may test our inputs and output methods. I recommend that you tackle the problem first before continuing.
Example:
// A normal IO example code
#include < bits/stdc++.h >
using namespace std ;
int main ( )
{
int n , k , t ;
int cnt = 0 ;
cin >> n >> k ;
for ( int i=0 ; i < n ; i++ )
{
cin >> t ;
if ( t % k == 0 )
cnt++ ;
}
cout << cnt << "\n" ;
return 0 ;
}
OUTPUT:
0
...
Process executed in 2.11 seconds
Press any key to continue.
Explanation
cin and cout are used in the code below. With a runtime of 2.11 seconds, the solution is acceptable.
Another Example:
// A fast IO program
#include < bits/stdc++.h >
using namespace std ;
int main ( )
{
// added the two lines below
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int n , k , t ;
int cnt = 0 ;
cin >> n >> k ;
for ( int i = 0 ; i < n ; i++ )
{
cin >> t ;
if ( t % k == 0 )
cnt++ ;
}
cout << cnt << "\n" ;
return 0 ;
}
OUTPUT:
0
...
Process executed in 0.22 seconds
Press any key to continue.
Explanation
By adding two lines, we can improve the performance and significantly cut the runtime. With a runtime of 0.22 seconds.
Another Example:
#include < bits/stdc++.h >
using namespace std;
void fastscan ( int &number )
{
//variable to indicate sign of input number
bool negative = false ;
register int c ;
number = 0 ;
// extract current character from buffer
c = getchar ( ) ;
if ( c == '-' )
{
// number is negative
negative = true ;
// extract the next character from the buffer
c = getchar ( ) ;
}
// Keep on extracting characters if they are integers
// i.e ASCII Value lies from '0'( 48 ) to '9' ( 57 )
for ( ; ( c > 47 && c < 58 ) ; c=getchar ( ) )
number = number *10 + c - 48 ;
// if scanned input has a negative sign, negate the
// value of the input number
if ( negative )
number *= -1 ;
}
// Function Call
int main ( )
{
int number ;
fastscan ( number ) ;
cout << number << "\n" ;
return 0 ;
}
OUTPUT:
0
...
Process executed in 0.12 seconds
Press any key to continue.
Explanation:
Now, when it comes to competitive challenges such as the ACM ICPC, Google CodeJam, and TopCoder Open, above is a unique code for reading numbers quickly.