There are few potential pitfalls with
- By default,
cin/coutwaste time synchronizing themselves with the C library’s stdio buffers, so that you can freely intermix calls to
scanf/printfwith operations on
cin/cout. Turn this off with
- Many C++ tutorials tell you to write
cout << endlinstead of
cout << '\n'. But
endlis actually slower because it forces a flush, which is usually unnecessary. (You’d need to flush right before prompting the user for input from
cin, but not when writing a million lines of output.) Write
- There was a bug in very old versions of GCC (pre-2004) that significantly slowed down C++ iostreams. Don’t use ancient compilers.
Avoid these pitfalls, and
cin/cout will be just as fast as
scanf/printf. My benchmarks indicate that
cin/cout is actually 5%–10% faster.