Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use <code>std::vector&lt;std::vector&lt;node&gt; &gt;</code>, but use <code>vector::reserve()</code> intelligently so as to avoid reallocations:</p> <pre><code>using std::vector; vector&lt;vector&lt;node&gt;&gt; v(d); // 0 nodes allocated, no wasted space yet using std::copy; using std::istream_iterator; using std::back_inserter; // Wish I could v[0].reserve(), but we don't have that info yet. copy(istream_iterator&lt;node&gt;(std::cin), istream_iterator&lt;node&gt;(), back_inserter(v[0])); // foreach v[1..d-1], compute v[i] from v[i-1] for(int i = 1; i &lt; d; ++i) { int size = ComputeSize(v[i-1]); // how big a list do we need? v[i].reserve(size); // reserve the space for it, and PopulateVectorFromVector(v[i-1], v[i]); // fill it in. } </code></pre> <p>The only vector with wasted space and time is v[0].</p> <p><hr /> <strong>EDIT</strong>: the call to <a href="http://www.cplusplus.com/reference/algorithm/copy/" rel="nofollow"><code>std::copy</code></a> above is a loop-less idiom roughly equivalent to </p> <pre><code>node tmp; while(std::cin &gt;&gt; tmp) { v[0].push_back(tmp); } </code></pre> <p><code>std::copy</code> takes three arguments. The first two are <a href="http://www.cplusplus.com/reference/std/iterator/InputIterator/" rel="nofollow">input iterators</a> which define the range from which we copy, while the third is an <a href="http://www.cplusplus.com/reference/std/iterator/OutputIterator/" rel="nofollow">output iterator</a> defining the destination.</p> <p>Specifically, <a href="http://www.cplusplus.com/reference/std/iterator/istream_iterator/" rel="nofollow"><code>istream_iterator&lt;node&gt;(std::cin)</code></a> is an input iterator which, when dereferenced, reads the next <code>node</code> from the standard input stream. <a href="http://www.cplusplus.com/reference/std/iterator/back_inserter/" rel="nofollow"><code>back_inserter(v[0])</code></a> is an output iterator which, when dereferenced and assigned to, invokes <a href="http://www.cplusplus.com/reference/stl/vector/push_back/" rel="nofollow"><code>v[0].push_back()</code></a>.</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