Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I create cartesian product of vector of vectors?
    primarykey
    data
    text
    <p>I've a vector of vectors say <code>vector&lt;vector&lt;int&gt; &gt; items</code> of different sizes like as follows</p> <pre><code>1,2,3 4,5 6,7,8 </code></pre> <p>I want to create combinations in terms of Cartesian product of these vectors like</p> <pre><code>1,4,6 1,4,7 1,4,8 and so on till 3,5,8 </code></pre> <p>How can I do that ? I've looked up several links and I've also listed them at the end of this post but I'm not able to interpret that as I'm not that familiar with the language. Could some body help me with this. </p> <pre><code>#include &lt;iostream&gt; #include &lt;iomanip&gt; #include &lt;vector&gt; using namespace std; int main() { vector&lt;vector&lt;int&gt; &gt; items; int k = 0; for ( int i = 0; i &lt; 5; i++ ) { items.push_back ( vector&lt;int&gt;() ); for ( int j = 0; j &lt; 5; j++ ) items[i].push_back ( k++ ); } cartesian ( items ); // I want some function here to do this. } </code></pre> <p>This program has equal length vectors and I put this so that it will be easier to understand my data structure. It will be very helpful even if somebody uses others answers from other links and integrate with this to get the result. Thank you very much</p> <p>Couple of links I looked at <a href="https://stackoverflow.com/questions/1700079/howto-create-combinations-of-several-vectors-without-hardcoding-loops-in-c">one</a> <a href="https://stackoverflow.com/questions/2405242/cartesian-product-of-several-vectors">Two</a> Program from : <a href="http://www.daniweb.com/software-development/cpp/threads/38527" rel="nofollow noreferrer">program</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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