I am writing a code segment that iterates through each permutation of n digits. So, for example, if n = 3, I would like to iterate over each of the following elements:
0, 0, 0
...
0, 1, 0
...
one hundred
...
2, 3, 4
...
9, 9, 9
This is very easy to code with nested loops:
for(digit1 0 to 9) for(digit2 0 to 9) for(digit3 0 to 9)
But I want to generalize this to n digits. If, for example, n = 10, I now need 10 nested loops.
I thought about it and realized that the problem can be solved using recursion (first, search for depth through a tree, with each node having 10 children, from 0 to 10 and stops at a depth of n). But I strive for high performance, so I do not want to use recursion due to overhead. What other alternatives do I have?
c ++ performance for-loop recursion tree
user1299784
source share