我的问题类似于这个组合问题,但在我的情况下,我有 N (N > 4) 个小集合(现在每组 1-2 个项目可能会变成 3 个或 4 个)并且想要从每个集合中生成一个项目的每个组合.
当前的解决方案看起来与此类似
for(T:: iterator a = setA.begin(); a != setA.end(); ++a)
for(T:: iterator b = setB.begin(); b != setB.end(); ++b)
for(T:: iterator c = setC.begin(); c != setC.end(); ++c)
for(T:: iterator d = setD.begin(); d != setD.end(); ++d)
for(T:: iterator e = setE.begin(); e != setE.end(); ++e)
something(*a,*b,*c,*d,*e);
简单,有效,可能相当有效,但丑陋且不可扩展。有谁知道更好/更清洁的方法来做到这一点,而且速度也一样快?
一个理想的解决方案看起来像一个单循环,并且来自一些支持良好的库。
Combinations<T> comb;
comb.set(0) = setA;
comb.set(1) = setB;
comb.set(2) = setC;
comb.set(3) = setD;
comb.set(4) = setE;
for(Combinations<T>::iterator a = comb.begin(); a != comb.end(); ++a)
something(*a[0],*a[1],*a[2],*a[3],*a[4]);