If you need to organize nested loops, then you only need an array to store the indices.
Suppose you need n nested loops, but the maximum value is m.
#include <iostream> #include <cmath> using namespace std; int main() { const int n = 3; const int m = 4; int ind[n] = {0}; int c = pow(m,n); // степень, может быть вычислена сразу. for (int i = 0; i < c; i++) { // это такое тело цикла - вывод индеков for (int j = 0; j < n; j++) { std::cout << ind[j] << " "; } std::cout << std::endl; // ключевая часть - обновим индексы for (int j = 0; j < n; j++) { ind[j]++; if (ind[j] < m) break; ind[j] = 0; } } return 0; }
If the indices should be different, then another array of indices is needed and the checks should not be with m, but with the elements of this array.
This code has many flaws, for example, you can get rid of the cycle parameter i (with large nesting there can be overflow). But it is easy if you want to modify. Also, you may need to use a vector. But I will not do all the code for you :)
In fact, it is identical to the usual positional number system.