[英]Multiple Nested For-loops without knowing the number of for-loops
how would I write n nested for loops without knowing what n is?在不知道 n 是什么的情况下,我将如何编写 n 个嵌套的 for 循环? For example, how would i write this using recursion or another method:
例如,我将如何使用递归或其他方法编写此代码:
for (int i = 0; cond1; i++){
for (int j = 0; cond2; j++){
for (int k = 0; cond3; k++)
...
for (int l = 0; cond_N; l++){
if (.....) break;
}
}
}
}
Here, there are n loops with some condition (not necessarily the same condition for each variable) and I'm not sure how to transform this into code using recursion without knowing what n is.在这里,有 n 个带有某些条件的循环(每个变量的条件不一定相同),我不确定如何在不知道 n 是什么的情况下使用递归将其转换为代码。 Thanks!
谢谢!
Is this what youre trying to do?这是你想要做的吗?
cond
provides the N different conditions (ie loop-variable-dependent bool function) and foo
introduces recursion: cond
提供了 N 个不同的条件(即依赖循环变量的 bool 函数)并且foo
引入了递归:
#include <iostream>
bool cond(int cond_which, int current_loop_variable) {
switch (cond_which) {
case 0:
return current_loop_variable < 5;
case 1:
return current_loop_variable < 3;
/* more... can be hella complicated, related with states, whatever */
default:
return false;
}
}
void foo(int we_may_call_it_the_meta_loop_variable) {
for (int i = 0; cond(we_may_call_it_the_meta_loop_variable, i); ++i) {
foo(we_may_call_it_the_meta_loop_variable + 1);
std::cout << "in loop " << we_may_call_it_the_meta_loop_variable << ", i = " << i << std::endl;
}
};
int main() {
foo(0);
return 0;
}
Clearly this is not an infinite recursion.显然,这不是无限递归。
Here is a code with a recursive loop
function.这是带有递归
loop
function 的代码。 Right now the condition is the same for each variable: it must be lower than MAX.现在,每个变量的条件都是一样的:它必须低于 MAX。 You could change it by passing to
loop
an additional array of function pointers that implement your conditions, ie, a function that takes as argument an integer (a loop variable) and returns 0 or 1.您可以通过传递给
loop
一个额外的 function 指针数组来更改它,这些指针实现了您的条件,即一个 function,它以 Z157DB7DF530023575515D366C9B672E1Z8 作为参数并返回(循环变量)。
#define MAX 3
int
loop
(int * indexes, /* stores loop variables i, j, k, ... */
int no_indexes /* number of loop variables */
) {
int i;
/* print the current values of loop variables for debugging */
printf("handle iteration ");
for(int i = 0; i < no_indexes; i ++) {
printf(" %d", indexes[i]);
}
printf("\n");
/* increment the last index variable and if reached MAX, reset it,
increment the previous one and do the same for the previous
one and so on */
indexes[no_indexes - 1] ++;
i = no_indexes - 1;
while(i > 0 && indexes[i] == MAX) {
indexes[i] = 0;
indexes[i - 1] ++;
i --;
}
/* recursive call if we have not terminated (i.e., if the first
index variable is < to MAX) */
if(indexes[0] != MAX) {
loop(indexes, no_indexes);
}
}
void
main
() {
/* test the function to simulate three nested loops */
int indexes[3] = { 0, 0, 0 };
loop(indexes, 3);
}
Also note that the recursion is not at all required here since the whole body of the function could be placed in a while loop instead.另请注意,这里根本不需要递归,因为 function 的整个主体可以放在 while 循环中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.