I need to iterate, for some values of k, on all combinations of taking k items out of a vector V containing N items. For example if N = 6 and k = 3 then I have to call F() on items V1, V2 and V[3], then on items V1, V2 and V[4], ending with V[4], V[5] and V[6].
Now, going through all combinations of the index is easy. Say k = 3 and N = 6. Then the first element can go from 1 to N-(k-1), the second from 2 to N-(k-2), the k'th from k to N (i.e. N-(k-k)).
So, for k = 3 the loop would be:
for (uint a = 1, a <= N-2, a++)
for (uint b = a + 1, b <= N-1, b++)
for (uint c = b + 1, c <= N, c++)
F(V[a], V[b], V[c])
and, for k = 4, the iteration would be:
for (uint a = 1, a <= N-3, a++)
for (uint b = a + 1, b <= N-2, b++)
for (uint c = b + 1, c <= N - 1, c++)
for (uint d = c + 1, d <= N, d++)
F(V[a], V[b], V[c], V[d])
The question, however, is: How do you accomplish this k-level nesting for an arbitrary k without hard coding it (or using code generation)?
Edit: For background (a.k.a. THIS IS NOT HOMEWORK :)) please see Hans Engler's answer to my Math Stackexchange quesion: 0-1 knapsack like - the set of all non-contained affordable binary selections
Edit: I'll provide a sample. Suppose V= {1,2,3,4,5}.
For k=2 I want F to be called in the following order: F(1,2), F(1,3), F(1,4), F(1,5), F(2,3), F(2,4), F(2,5), F(3,4), F(3, 5) and F(4,5).
For k=3 I want F to be called in the following order: F(1,2,3), F(1,2,4), F(1,2,5), F(2,3,4,), F(2,3,5), F(3,4,5)