Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="http://www.cplusplus.com/reference/std/iterator/istream_iterator/" rel="nofollow"><code>istream_iterator</code></a> allows you to iteratively extract items from an <a href="http://www.cplusplus.com/reference/iostream/istream/" rel="nofollow"><code>istream</code></a>, which you pass in to the constructor. The <code>eof</code> object is explained thus:</p> <blockquote> <p>A special value for this iterator exists: the end-of-stream; When an iterator is set to this value has either reached the end of the stream (operator void* applied to the stream returns false) or has been constructed using its default constructor (without associating it with any basic_istream object).</p> </blockquote> <p><a href="http://www.cplusplus.com/reference/algorithm/for_each/" rel="nofollow"><code>for_each</code></a> is a loop construct that takes iterator #1 and increments it until it becomes equal with iterator #2. Here it takes the iterator that wraps standard input <a href="http://www.cplusplus.com/reference/iostream/cin/" rel="nofollow"><code>cin</code></a> and increments it (which translates to extracting items) until there is no more input to consume -- this makes <code>input</code> compare equal to <code>eof</code> and the loop ends.</p> <p>The construct <code>[&amp;] (int x) { frequency[x]++; }</code> is an <a href="http://en.wikipedia.org/wiki/Anonymous_function#C.2B.2B" rel="nofollow">anonymous function</a>; it is simply a shorthand way to write functions inline. Approximately the same effect could be achieved with</p> <pre><code>unordered_map&lt;int,int&gt; frequency; // this NEEDS to be global now istream_iterator&lt;int&gt; input(cin); istream_iterator&lt;int&gt; eof; void consume(int x) { frequency[x]++; } for_each(input, eof, consume); </code></pre> <p>So in a nutshell: this code reads integers from standard input until all available data is consumed, keeping a count of each integer's appearance frequency in a map.</p>
 

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