Don’t Use cin and cout in C++

rajat aggarwal
2 min readAug 17, 2020
Photo by Markus Spiske on Unsplash

This is the case for competitive programming. In competitive, the cin and cout command tends to take more execution time than its counterparts scanf and printf.

The scanf and printf command may usually be used in C language but it tends to perform better and faster than other commands. But the problem which arises with using print is that we have to remember each and keyword for each data type. Such as :

int x = 5;
printf("%d",x);

As in this, for int data type, it ‘d’ for float it is ‘f’ along with the number of decimal places we want to include in it.

printf("%0.2f",x);

This command will print only up to 2 decimal places for the floating type number.

Overall (maybe I just went a little too ahead with the commands), As of writing and remembering these new syntaxes may take up more of our time and as time is very precious in any competition. So the solution to this problem is to use the ios base command:

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

This may look a bit over the top or a bit strange. But this command just has a simple functionality such that it just converts any cin or cout command to scanf and printf respectively in the background. The benefits of using this are:

  • We can keep using our old cin and cout commands
  • We can enjoy the execution time of scanf and printf without actually even either implementing or remembering the syntax.

--

--

rajat aggarwal

Software Engineer , hope you’ll appreciate the content. Also follow me on vocal @ https://vocal.media/authors/rajat-ir2e1e0bfm