C++ - recursion for loop - how to get access to various iterators

I would like to make recursion in for loop with access to following iterators independently.
I tried to do that, but it looks like nested loop doesn't update its parent iterator. I am not sure what's wrong here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
        std::vector<int> it;
        // some code that fill "it" vector

        std::vector<int *> iter;
        for(int i=0; i<it.size(); i++)
        {
            iter.push_back(&it(i));
        }
        
        int sweek = 0;
        int dup=0;
        
        // And now call the function which definition is:
        void matrixRecursion()
        {
            int start = sweek;
            int end = matrix[sweek];

            for(it(start); it(start)<end; it(start)++)
            {
                if(sweek < it.size()-1)
                {
                    sweek++;
                    matrixRecursion();
                }
                else
                {
                    for(int t=0; t<it.size(); t++)
                    {
                        dup += nIndex[*iter[t]];
                    }
                }
            }
        }


For example lets say it.size() is 3. Then if everything would work as I expect the result should be like that:
1
2
3
4
5
6
7
8
9
10
    for(int i=0; i<matrix[0]; i++)
    {
        for(int j=0; j<matrix[1]; j++)
        {
            for(int k=0; k<matrix[2]; k++)
            {
                dup += nIndex[i] + nIndex[j] + nIndex[k];
            }
        }
    }

Last edited on
for(it(start); it(start)<end; it(start)++)

it is a vector<int>, and start is an int. I have no idea what it(start) is meant to be. I don't see how it compiles.
Last edited on
Hmm,
1
2
3
4
5
6
7
8
9
10
    for(int i=0; i<matrix[0]; i++)
    {
        for(int j=0; j<matrix[1]; j++)
        {
            for(int k=0; k<matrix[2]; k++)
            {
                dup += nIndex[i] + nIndex[j] + nIndex[k];
            }
        }
    }

Could be:
1
2
3
4
5
6
7
8
9
10
11
12
for ( int i=0; i<matrix[0]; i++ )
{
  dup += nIndex[i] * matrix[1] * matrix[2];
  for ( int j=0; j<matrix[1]; j++ )
  {
    dup += nIndex[j] * matrix[2];
    for ( int k=0; k<matrix[2]; k++ )
    {
      dup += nIndex[k];
    }
  }
}

However, the innermost loop is simply repeated matrix[0]*matrix[1] times:
1
2
3
4
5
6
7
8
9
10
11
12
13
for ( int i=0; i<matrix[0]; i++ )
{
  dup += nIndex[i] * matrix[1] * matrix[2];
  for ( int j=0; j<matrix[1]; j++ )
  {
    dup += nIndex[j] * matrix[2];
  }
}

for ( int k=0; k<matrix[2]; k++ )
{
  dup += nIndex[k] * matrix[0] * matrix[1];
}

Similar for the remaining nested loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for ( int i=0; i<matrix[0]; i++ )
{
  dup += nIndex[i] * matrix[1] * matrix[2];
}

for ( int j=0; j<matrix[1]; j++ )
{
  dup += nIndex[j] * matrix[0] * matrix[2];
}

for ( int k=0; k<matrix[2]; k++ )
{
  dup += nIndex[k] * matrix[0] * matrix[1];
}

Still too much repetition.
1
2
3
4
5
6
7
8
9
10
foo[0] = 0;
for ( int i=0; i<matrix[0]; i++ )
{
  foo[0] += nIndex[i];
}
foo[0] *= matrix[1] * matrix[2];

...

dup += foo[0] + foo[1] + foo[2];

That -- unless my refactoring is flawed -- is a quite different base to start recursion from.
@Repeater - it(start) is my fault of course, sorry. I meant it[start]. And I mean by that just address of it[somevalue] which is pointing by iter[somevalue] and then I have easy access to each iterator by calling value under the pointed address, by calling *iter[somevalue]. But unfortunately it doesn't work. I don't know why. I suppose its some misunderstanding about recursion, I am not sure how it works. All iterations happen as I expect, just *iter[somevalue] which is called in last/deepest nested loop is not updating.
Topic archived. No new replies allowed.