Possible Duplicate:
How can I create cartesian product of vector of vectors?
I'm having some logical issues figuring out how to generate all combinations of elements in a 2d vector. Here, I create a 2D vector. Neither dimension's size can be assumed.
#include <iostream>
#include <vector>
using namespace std;
int main() {
srand(time(NULL));
vector< vector<int> > array;
// This creates the following:
// array[0]: {0, 1, 2}
// array[1]: {3, 4, 5, 9}
// array[2]: {6, 7, 8}
for(int i=0; i<3; i++) {
vector<int> tmp;
tmp.push_back((i*3)+0); tmp.push_back((i*3)+1); tmp.push_back((i*3)+2);
if(i==1)
tmp.push_back((i*3)+6);
array.push_back(tmp);
}
}
After creating the vector, I'd like to output all possible combinations as follows:
comb[0] = {0, 3, 6}
comb[1] = {0, 3, 7}
comb[2] = {0, 3, 8}
comb[3] = {0, 4, 6}
comb[4] = {0, 4, 7}
comb[x] = {...}
However, I am having trouble how to conceptualize the loop structure to do this properly, where the size 'array' and the elements in each sub-array is unknown/dynamic.
EDIT 1: Can't assume there are 3 arrays. There are array.size() of them ;)