Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First, I'll show you a recursive version. </p> <pre><code>// Cartesion product of vector of vectors #include &lt;vector&gt; #include &lt;iostream&gt; #include &lt;iterator&gt; // Types to hold vector-of-ints (Vi) and vector-of-vector-of-ints (Vvi) typedef std::vector&lt;int&gt; Vi; typedef std::vector&lt;Vi&gt; Vvi; // Just for the sample -- populate the intput data set Vvi build_input() { Vvi vvi; for(int i = 0; i &lt; 3; i++) { Vi vi; for(int j = 0; j &lt; 3; j++) { vi.push_back(i*10+j); } vvi.push_back(vi); } return vvi; } // just for the sample -- print the data sets std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Vi&amp; vi) { os &lt;&lt; "("; std::copy(vi.begin(), vi.end(), std::ostream_iterator&lt;int&gt;(os, ", ")); os &lt;&lt; ")"; return os; } std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Vvi&amp; vvi) { os &lt;&lt; "(\n"; for(Vvi::const_iterator it = vvi.begin(); it != vvi.end(); it++) { os &lt;&lt; " " &lt;&lt; *it &lt;&lt; "\n"; } os &lt;&lt; ")"; return os; } // recursive algorithm to to produce cart. prod. // At any given moment, "me" points to some Vi in the middle of the // input data set. // for int i in *me: // add i to current result // recurse on next "me" // void cart_product( Vvi&amp; rvvi, // final result Vi&amp; rvi, // current result Vvi::const_iterator me, // current input Vvi::const_iterator end) // final input { if(me == end) { // terminal condition of the recursion. We no longer have // any input vectors to manipulate. Add the current result (rvi) // to the total set of results (rvvvi). rvvi.push_back(rvi); return; } // need an easy name for my vector-of-ints const Vi&amp; mevi = *me; for(Vi::const_iterator it = mevi.begin(); it != mevi.end(); it++) { // final rvi will look like "a, b, c, ME, d, e, f" // At the moment, rvi already has "a, b, c" rvi.push_back(*it); // add ME cart_product(rvvi, rvi, me+1, end); add "d, e, f" rvi.pop_back(); // clean ME off for next round } } // sample only, to drive the cart_product routine. int main() { Vvi input(build_input()); std::cout &lt;&lt; input &lt;&lt; "\n"; Vvi output; Vi outputTemp; cart_product(output, outputTemp, input.begin(), input.end()); std::cout &lt;&lt; output &lt;&lt; "\n"; } </code></pre> <p>Now, I'll show you the <strike>recursive</strike> iterative version that I shamelessly stole from @John :</p> <p>The rest of the program is pretty much the same, only showing the <code>cart_product</code> function.</p> <pre><code>// Seems like you'd want a vector of iterators // which iterate over your individual vector&lt;int&gt;s. struct Digits { Vi::const_iterator begin; Vi::const_iterator end; Vi::const_iterator me; }; typedef std::vector&lt;Digits&gt; Vd; void cart_product( Vvi&amp; out, // final result Vvi&amp; in) // final result { Vd vd; // Start all of the iterators at the beginning. for(Vvi::const_iterator it = in.begin(); it != in.end(); ++it) { Digits d = {(*it).begin(), (*it).end(), (*it).begin()}; vd.push_back(d); } while(1) { // Construct your first product vector by pulling // out the element of each vector via the iterator. Vi result; for(Vd::const_iterator it = vd.begin(); it != vd.end(); it++) { result.push_back(*(it-&gt;me)); } out.push_back(result); // Increment the rightmost one, and repeat. // When you reach the end, reset that one to the beginning and // increment the next-to-last one. You can get the "next-to-last" // iterator by pulling it out of the neighboring element in your // vector of iterators. for(Vd::iterator it = vd.begin(); ; ) { // okay, I started at the left instead. sue me ++(it-&gt;me); if(it-&gt;me == it-&gt;end) { if(it+1 == vd.end()) { // I'm the last digit, and I'm about to roll return; } else { // cascade it-&gt;me = it-&gt;begin; ++it; } } else { // normal break; } } } } </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