title: C++ How to traverse through different set elements of a vector in specific pattern
body: <p>I have a vector of sets which contain integers as given below:</p>
  
  <pre><code>std::vector&lt;std::set&lt;int&gt; &gt; vec = {{2,4},{1,3,8},{7,5}};
  </code></pre>
  
  <p>Diagram illustration of the above vector of set</p>
  
  <pre><code>| 2 | 4 |       
  | 1 | 3 | 8 |
  | 7 | 5 |
  </code></pre>
  
  <p>I need to traverse through each set elements of vector such that each set element in vector row visits every set element in the next row and so on. </p>
  
  <p>For example, the first element of the set in first row of vector (i.e 2) will visit the first element of second row (i.e 1) and then visit the first element of third row (i.e 7). Similarly, these traversals will be in the order as below: </p>
  
  <pre><code>First vector row and first set element -&gt; Second vector row and first set element -&gt; Third vector row and first set element
  First vector row and first set element -&gt; Second vector row and first set element -&gt; Third vector row and second set element
  First vector row and first set element -&gt; Second vector row and second set element -&gt; Third vector row and first set element
  First vector row and first set element -&gt; Second vector row and second set element -&gt; Third vector row and second set element 
  First vector row and first set element -&gt; Second vector row and third set element -&gt; Third vector row and first set element
  First vector row and first set element -&gt; Second vector row and third set element -&gt; Third vector row and second set element
  
  First vector row and second set element -&gt; Second vector row and first set element -&gt; Third vector row and first set element
  First vector row and second set element -&gt; Second vector row and first set element -&gt; Third vector row and second set element
  First vector row and second set element -&gt; Second vector row and second set element -&gt; Third vector row and first set element
  First vector row and second set element -&gt; Second vector row and second set element -&gt; Third vector row and second set element 
  First vector row and second set element -&gt; Second vector row and third set element -&gt; Third vector row and first set element
  First vector row and second set element -&gt; Second vector row and third set element -&gt; Third vector row and second set element
  </code></pre>
  
  <p>The resultant vector should be a vector of list with each of its elements as below:</p>
  
  <pre><code>std::vector&lt;std::list&lt;int&gt; &gt; list = {{2,1,7},{2,1,5},{2,3,7},{2,3,5},{2,8,7},{2,8,5},{4,1,7},{4,1,5},{4,3,7},{4,3,5},{4,8,7},{4,8,5}};
  </code></pre>
  
  <p>Diagram illustration of the resultant vector of list</p>
  
  <pre><code>| 2 | 1 | 7 |
  | 2 | 1 | 5 | 
  | 2 | 3 | 7 |
  | 2 | 3 | 5 |
  | 2 | 8 | 7 |
  | 2 | 8 | 5 |
  | 4 | 1 | 7 |
  | 4 | 1 | 5 | 
  | 4 | 3 | 7 |
  | 4 | 3 | 5 |
  | 4 | 8 | 7 |
  | 4 | 8 | 5 |
  </code></pre>
  
  <p>What would be the most efficient way to achieve this in C++? </p>