I am currently needing to create a loop that deals with N iterators. The real purpose is to create a series of names which will then be used to open files with such names. But anyways...
For example, imagine you have an input such as
The program should then create the following names:
A1B4C6
A1B5C6
A2B4C6
A2B5C6
A3B4C6
A3B5C6 |
That is, the
%
is a placeholder for the values in each parentheses.
In this case, the best loop would be (in pseudo-code)
1 2 3 4
|
for (each A)
for(each B)
for(each C)
CreateName();
|
However, that is hard-written as a three-iterator loop. What if the filename is only "A%B%"? What if it's a crazy "A%B%C%D%E%F%G%"?
I've tried writing something like
1 2 3 4
|
for(each position)
for(each position after this position)
for(each value of the position)
CreateName();
|
But it doesn't seem to work. To keep things a bit more realistic, here's my actual code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
for(i=count-2;i>=0;i--)
{
for(int j=count-1;j>i;j--)
{
stringstream _substr(substr[j]);
string value;
while(_substr >> value)
{
name.replace(pos[j],1,value);
cout << name << endl;
}
}
}
|
Where
substr
contains all the values of each parentheses ( 1 2 3 and 4 5 and 6 in different strings) and name contains the "first value" (A1B4C6)
However, this doesn't work. It loops too much in the beginning, repeating itself, and loops too little in the end.