在 c++ 中创建递归 for 语句

Create a recursive for statement in c++

本文关键字:for 语句 递归 创建 c++      更新时间:2023-10-16

我想知道是否真的可以在C++中创建/定义自制的for语句。这里已经问过类似的事情:

"如何在C++中创建类似 for 循环的命令?#user9282 的回答">

我的要求是,我们是否可以制作一个for,它可以执行我们想要的for(n次(。

例如,下面是一个基本的 for 循环语句:

for (int i = 0; i < 10; i++) { ... }

我想知道新的 for 循环是否会产生这样的结果

int x = 20; // individual loops
int y = 3; // = amount of for-loops performed (in this case, 3)
// maybe some code generating for-loops here or something...
// result:
for (int i = 0; i < x; i++)
{
    for (int j = 0; j < x; j++)
    {
        for (int k = 0; k < x; k++)
        {
            // if "y" was equal to 5, there would be 2 more for-loops here
            instructions; // we can use "i", "j", "k", ... here
        }
    }
}

你认为这在 C++ 中可能吗?

[编辑:使上面的代码更清晰]

一句话:我想创建一个语句(例如,if,while,for,switch(,将for循环放入for循环中(就像上面的代码将for循环放入for循环中一样(,这样我们就可以在同一范围内访问多个增量(i,j,k,...(。

您可以使用包含 for 循环的递归函数轻松做到这一点。我会这样做:

void foo() {
    for (...) {
        foo();
    }
}

这样,您可以根据需要执行任意数量的嵌套循环。

但是,如果要在不定义外部函数的情况下在代码中定义嵌套的递归循环,则可以使用 lambdas:

auto recursive = [](auto func) {
    // This is needed to make a recursive lambda
    return [=](auto... args){
        func(func, args...);
    };
};
auto nestedForLoop = recursive([](auto self){
    // Here's your recursive loop!
    for (...) {
        self();
    }
});
// You can simply call `nestedForLoop` to execute loop
nestedForLoop();

如果您有 n 具有相同绑定 x 的嵌套for循环,则正在执行 xn 次迭代。在这种情况下,您可以只使用单个索引并将其转换为多个索引以方便使用。例如,对于 n = 2:

for (int z = 0; z < x * x; ++z) {
  const int i = z / x;
  const int j = z % x;
  // ...
}

n = 3 时:

for (int z = 0; z < x * x * x; ++z) {
  // The last "% x" in this line is for illustration only.
  const int i = (z / x / x) % x;
  const int j = (z / x) % x;
  const int k = z % x;
  // ...
}

ijk等是当前迭代次数z转换为基数x的数字。您可以将其推广为递归函数,或者将数字解压缩为vector函数。

快速回答:

#include <iostream>
using namespace std;
void recursive(int x, int y, int tempY) // 2d recursive for loop 
{
    if (x > 0)
    {
        if (y > 0)
        {
            cout << x << " " << y << " n ";
            recursive(x, y - 1, tempY);
        }
        else
        {
            y = tempY;
            recursive(x - 1, y, tempY);
        }
    }
}
void recursive(int x, int y, int z, int tempY, int tempZ) // 3d recursive for loop
{
    if (x > 0)
    {
        if (y > 0)
        {
            if (z > 0)
            {
                cout << x << " " << y << " " << z << " n ";
                recursive(x, y, z - 1, tempY, tempZ);
            }
            else
            {
                z = tempZ;
                recursive(x, y - 1, z, tempY, tempZ);
            }
        }
        else
        {
            y = tempY;
            recursive(x - 1, y, z, tempY, tempZ);
        }
    }
}
int main()
{ 
    recursive(1, 2, 2); // x = 1, y = 2
    recursive(1, 2, 3, 2, 3); // x = 1, y = 2, z = 3
}

快速回答:

void n_for_loop(int x, int size, int arr[])
{
    static int* arrTemp(new int[size]);
    if (x >= size)
    {
        for (int i = 0; i < size; i++)
        {
            cout << arrTemp[i] << " ";
        }
        cout << endl;
        return;
    }
    for (int i = 0; i < arr[x]; i++)
    {
        arrTemp[x] = i;
        n_for_loop(x + 1, size, arr);
    }
}