Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To answer the original question:<br> Anything that can be done using stdio can be done using the iostream library.<br></p> <pre><code>Disadvantages of iostreams: verbose Advantages of iostreams: easy to extend for new non POD types. </code></pre> <p>The step forward the C++ made over C was type safety.</p> <ul> <li><p>iostreams was designed to be explicitly type safe. Thus assignment to an object explicitly checked the type (at compiler time) of the object being assigned too (generating an compile time error if required). Thus prevent run-time memory over-runs or writing a float value to a char object etc.<br></p></li> <li><p>scanf()/printf() and family on the other hand rely on the programmer getting the format string correct and there was no type checking (I believe gcc has an extension that helps). As a result it was the source of many bugs (as programmers are less perfect in their analysis than compilers [not going to say compilers are perfect just better than humans]).</p></li> </ul> <p>Just to clarify comments from Colin Jensen.</p> <ul> <li>The iostream libraries have been stable since the release of the last standard (I forget the actual year but about 10 years ago).</li> </ul> <p>To clarify comments by Mikael Jansson.</p> <ul> <li>The other languages that he mentions that use the format style have explicit safeguards to prevent the dangerous side effects of the C stdio library that can (in C but not the mentioned languages) cause a run-time crash.</li> </ul> <p><b>N.B.</b> I agree that the iostream library is a bit on the verbose side. But I am willing to put up with the verboseness to ensure runtime safety. But we can mitigate the verbosity by using <a href="http://www.boost.org/doc/libs/1_36_0/libs/format/doc/format.html" rel="nofollow noreferrer">Boost Format Library</a>.</p> <pre><code>#include &lt;iostream&gt; #include &lt;iomanip&gt; #include &lt;boost/format.hpp&gt; struct X { // this structure reverse engineered from // example provided by 'Mikael Jansson' in order to make this a running example char* name; double mean; int sample_count; }; int main() { X stats[] = {{"Plop",5.6,2}}; // nonsense output, just to exemplify // stdio version fprintf(stderr, "at %p/%s: mean value %.3f of %4d samples\n", stats, stats-&gt;name, stats-&gt;mean, stats-&gt;sample_count); // iostream std::cerr &lt;&lt; "at " &lt;&lt; (void*)stats &lt;&lt; "/" &lt;&lt; stats-&gt;name &lt;&lt; ": mean value " &lt;&lt; std::fixed &lt;&lt; std::setprecision(3) &lt;&lt; stats-&gt;mean &lt;&lt; " of " &lt;&lt; std::setw(4) &lt;&lt; std::setfill(' ') &lt;&lt; stats-&gt;sample_count &lt;&lt; " samples\n"; // iostream with boost::format std::cerr &lt;&lt; boost::format("at %p/%s: mean value %.3f of %4d samples\n") % stats % stats-&gt;name % stats-&gt;mean % stats-&gt;sample_count; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload