Editing PEG:C++ Lesson 6 for Pascal Users

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 8: Line 8:
 
More information, including additional format specifiers (yes, there is one for every basic type in C --- but note that <code>bool</code> did not exist in C and was introduced in C++) and cool features can be found on the appropriate pages of [http://www.cplusplus.com http://www.cplusplus.com].<br><br>
 
More information, including additional format specifiers (yes, there is one for every basic type in C --- but note that <code>bool</code> did not exist in C and was introduced in C++) and cool features can be found on the appropriate pages of [http://www.cplusplus.com http://www.cplusplus.com].<br><br>
 
The third set of I/O functions is the set that involves <code>cin</code> and <code>cout</code>, defined in the <code>iostream</code> header. (<code>iostream</code> includes <code>stdio.h</code> automatically.) These were introduced with C++. The insertion and extraction operators usually suffice, but there are additional functions such as <code>getline()</code>, <code>cin.getline()</code>, and <code>cin.ignore()</code> that you may want to read up on. Controlling the format of output cannot be done as elegantly as it can with <code>printf();</code> instead, the header <code>iomanip</code> provides a solution to this task. I will assume that you are all competent users of search engines and leave it to you to learn how to use <code>iomanip</code>, should the need arise.<br><br>
 
The third set of I/O functions is the set that involves <code>cin</code> and <code>cout</code>, defined in the <code>iostream</code> header. (<code>iostream</code> includes <code>stdio.h</code> automatically.) These were introduced with C++. The insertion and extraction operators usually suffice, but there are additional functions such as <code>getline()</code>, <code>cin.getline()</code>, and <code>cin.ignore()</code> that you may want to read up on. Controlling the format of output cannot be done as elegantly as it can with <code>printf();</code> instead, the header <code>iomanip</code> provides a solution to this task. I will assume that you are all competent users of search engines and leave it to you to learn how to use <code>iomanip</code>, should the need arise.<br><br>
I took the time to explain <code>scanf()</code> and <code>printf()</code> not because of their elegance, though. Rather, it is because in g++ versions before 4.1 (''I think''), <code>cin >></code> was very slow, and <code>cout <<</code> somewhat slow. So slow, in fact, that it is actually possible, on some problems, to TLE just from reading input. (An example is {{problem|ccc09s4|Ship & Shop}}.) When working a problem from somewhere other than the PEG Judge that has large cases (''i.e.'', more than 50 kB input or 500 kB output), I strongly recommend that you play it safe by using <code>scanf()</code> and <code>printf()</code> instead of <code>cin >></code> and <code>cout <<</code>.<br><br>
+
I took the time to explain <code>scanf()</code> and <code>printf()</code> not because of their elegance, though. Rather, it is because in g++ versions before 4.1 (''I think''), <code>cin >></code> was very slow, and <code>cout <<</code> somewhat slow. So slow, in fact, that it is actually possible, on some problems, to TLE just from reading input. (An example is [http://pegjudge.ath.cx:5050/problem/ccc09s4 Ship & Shop].) When working a problem from somewhere other than the PEG Judge that has large cases (''i.e.'', more than 50 kB input or 500 kB output), I strongly recommend that you play it safe by using <code>scanf()</code> and <code>printf()</code> instead of <code>cin >></code> and <code>cout <<</code>.<br><br>
 
What about on the Judge? In recent g++ versions, including the 4.1 used by the Judge and beyond, the design flaws of <code>cin >></code> and <code>cout <<</code> have been largely fixed. (I do not know if they persist in Visual C++, but then again online judges don't use that anyway.) However, they are still slow because they have to constantly ''synchronize'' with the I/O functions provided by C. You can circumvent this by promising the program that you won't use any C input functions, and using the statement <code>cin.sync_with_stdio(false);</code>; similarly, if you won't use any C output functions, you can use <code>cout.sync_with_stdio(false);</code>. These increase the speed of <code>cin >></code> and <code>cout << </code> (respectively) dramatically on the recent g++ versions, so that they should be fast enough to solve any problem; in fact, they are often faster than <code>scanf()</code> and <code>printf()</code>. (If you break your promise, though, you will get some nasty error like SIGSEGV, or your program just won't work properly and you'll get WA.)<br><br>
 
What about on the Judge? In recent g++ versions, including the 4.1 used by the Judge and beyond, the design flaws of <code>cin >></code> and <code>cout <<</code> have been largely fixed. (I do not know if they persist in Visual C++, but then again online judges don't use that anyway.) However, they are still slow because they have to constantly ''synchronize'' with the I/O functions provided by C. You can circumvent this by promising the program that you won't use any C input functions, and using the statement <code>cin.sync_with_stdio(false);</code>; similarly, if you won't use any C output functions, you can use <code>cout.sync_with_stdio(false);</code>. These increase the speed of <code>cin >></code> and <code>cout << </code> (respectively) dramatically on the recent g++ versions, so that they should be fast enough to solve any problem; in fact, they are often faster than <code>scanf()</code> and <code>printf()</code>. (If you break your promise, though, you will get some nasty error like SIGSEGV, or your program just won't work properly and you'll get WA.)<br><br>
 
Notice that the <code>sync_with_stdio(false)</code> trick doesn't work particularly well on judges with older g++ versions, so, as I said, only use it on the PEG Judge, unless you are absolutely sure that the judge to which you submit your code has one of the newer g++ versions. On older versions, <code>scanf()</code> and <code>printf()</code> are often your only reasonable choices.
 
Notice that the <code>sync_with_stdio(false)</code> trick doesn't work particularly well on judges with older g++ versions, so, as I said, only use it on the PEG Judge, unless you are absolutely sure that the judge to which you submit your code has one of the newer g++ versions. On older versions, <code>scanf()</code> and <code>printf()</code> are often your only reasonable choices.

Please note that all contributions to PEGWiki are considered to be released under the Attribution 3.0 Unported (see PEGWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)