Skip to main content

12.8) Efficient Use of Standard Streams


By default, the standard C++ streams cin, cout, cerr, clog all synchronize their behaviour with the equivalent streams used by C. In practice, what this means is that C++ and C input/output operations using these streams share common buffers for the storage of data being read/written.

Synchronization allows you to freely mix the C and C++ approaches to I/O in your code, but at the cost of making the C++ streams less efficient. If you only ever use the C++ approach, you can improve the performance of your code by disabling synchronization like so:

cin.sync_with_stdio(false);
cout.sync_with_stdio(false);