Don’t Use cin and cout in C++

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 :

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.

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:

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.

Data Scientist Blogger , hope you’ll appreciate the content

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store