可能重复:
How can I create cartesian product of vector of vectors?生成元素的所有组合中的2D矢量
我在搞清楚如何产生二维向量元素的所有组合逻辑的一些问题。在这里,我创建了一个2D矢量。尺寸的大小都不能被假定。
#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);
}
}
创建矢量之后,我想输出所有可能的组合如下:
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] = {...}
不过,我有麻烦如何概念化循环结构正确,在执行此操作大小'数组',每个子数组中的元素是未知/动态的。编辑1:不能假设有3个数组。它们有array.size();)
我可以帮助你,但请解释更多[数学](http://www.mathsisfun.com/combinatorics/combinations-permutations.html)你的意思。 – Kos 2011-12-23 20:24:54
它也许是一个carthesian产品?对于5个数组ABCDE作为输入,你会期望所有可能的5元组(abcde),其中a来自A,b来自B等? – Kos 2011-12-23 20:25:43