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:
Code:
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:
Code:
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];
}
}
}
Re: C++ - recursion for loop - how to get access to various iterators
Quote:
for(it(start); it(start)<end; it(start)++)
Maybe you should explain what you think this line of code is supposed to do. std::vector doesn't have an operator()( int ). Even if it did, what do you expect this code to do? "it" is a vector, and even if "it( start )" returned some member of that vector, your for() loop doesn't save it anywhere so it can use it later so the statement is essentially a no-op.
I think you are basically confused about what iterators are and how you use them. In any case, this post is not about Qt, it is basic C++ knowledge and is off-topic for this forum.