我只是寫出一個可能的答案,這不是非常有效,但應該是可用的。
請注意,我假設(在你的例子),你希望所有的5元組,從V1拍攝的第一要素,從V2拍攝的第二要素,第三從V3等
void gen_all (
vector<vector<int> > & output_perms,
vector<vector<int> > const & input,
vector<int> & cur_perm,
unsigned cur_row = 0
)
{
if (cur_row >= input.size())
{
// This is where you have found a new permutation.
// Do whatever you want with it.
output_perms.push_back (cur_perm);
return;
}
for (unsigned i = 0; i < input[cur_row].size(); ++i)
{
cur_perm.push_back (input[cur_row][i]);
gen_all (output_perms, input, cur_perm, cur_row + 1);
cur_perm.pop_back();
}
}
這樣調用上面的函數:(假設v
握着你的原套)
vector<vector<int> > output;
vector<int> temp;
gen_all (output, v, temp);
正如我之前說的,有更有效和優雅的方式,和上面的代碼可能甚至不編譯(我只是寫這裏)。
來源
2012-06-18 22:18:52
yzt
[數組上的Perfoming Cartesian產品]可能的副本(http://stackoverflow.com/questions/10840430/perfoming-cartesian-product-on-arrays) – templatetypedef
您在尋找的不是子集,而是笛卡爾積(從每個元素中挑選一個元素的所有方法)。 – templatetypedef
@templatetypedef啊,我改正了標題。謝謝 – SamTheSammich