Interleaving the standard streams with sync_with_stdio()

The sync_with_stdio() function allows you to interleave C standard streams with standard streams from either the Standard C++ Library or the USL I/O Stream Class Library. A call to sync_with_stdio() does the following:
  • cin, cout, cerr, and clog are initialized with stdiobuf objects associated with stdin, stdout, and stderr.
  • The flags unitbuf and stdio are set for cout, cerr, and clog.

This ensures that subsequent standard streams may be mixed on a per-character basis. However, a runtime performance penalty is incurred to ensure this synchronization. Figure 1 shows an example program and the output that it produces.