0

I have the following Problem: Consider a Vector of Vector, such as

[[p00, p01, p02], [p10, p11], [p20, p21, p22, p23]]

I have to find every possible combination of each element, i.e.

[[p00, p10, p20], [p00, p10, p21], ... [p00, p10, p23], [p00, p11, p20], ... ]

In general I do have a vector<vector< MyClass >> mat with mat.size() = d and each combination should be a vector< MyClass > comb1 with comb1.size() = d.

I have found several threads, such as this solution, where each List inside the mat is the same size. However, using DuckDuckGo I was not able to find a proper solution that works for my case, where each vector can have different sizes. I also need to save the resulting vectors, in an extra matrix or directly link it to what I need.

I also figured out, it is the best way to somehow use a recursion. However, I already fail to see what kind of termination criterion I need, since in the link above, we had a fixed size for each entry of mat.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
ro_go
  • 90
  • 7
  • *where each List inside the mat is the same size* -- The easiest solution I could think of is to just make your matrix have the same number of entries also, and fill in the extra spaces with junk. Then after generating the combinations, remove the junk entries from the output. – PaulMcKenzie Mar 02 '20 at 16:52
  • Make a "combinations" class with an underlying `std::vector` data member that represents the current indices of each sub vector. Make an increment function that increases the index (`{0,0,0} -> {0,0,1}` with rollover). Make a "get_next_combination" function that increments the indices, then returns a vector with all all the pulled values. – JohnFilleau Mar 02 '20 at 16:52

0 Answers0