在 C++ 中创建递归 for 语句
Create a recursive for statement in c++
我想知道是否真的可以在 C++ 中创建/定义一个自制的 for
语句。这里已经问过类似的问题:
"How to create a for loop like command in C++? #user9282's answer"
我要求的是,我们是否可以制造一个 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-loops(就像上面的代码有for-loops into for-loops)所以我们可以访问多个增量(i,j, k, ...) 在相同范围.
您可以使用包含 for 循环的递归函数轻松地做到这一点。我会这样做:
void foo() {
for (...) {
foo();
}
}
这样,您可以根据需要进行尽可能多的嵌套 for 循环。
但是,如果您想在代码中定义递归嵌套 for 循环而不定义外部函数,您可以使用 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;
// ...
}
i
、j
、k
等为当前迭代数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);
}
}
我想知道是否真的可以在 C++ 中创建/定义一个自制的 for
语句。这里已经问过类似的问题:
"How to create a for loop like command in C++? #user9282's answer"
我要求的是,我们是否可以制造一个 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-loops(就像上面的代码有for-loops into for-loops)所以我们可以访问多个增量(i,j, k, ...) 在相同范围.
您可以使用包含 for 循环的递归函数轻松地做到这一点。我会这样做:
void foo() {
for (...) {
foo();
}
}
这样,您可以根据需要进行尽可能多的嵌套 for 循环。
但是,如果您想在代码中定义递归嵌套 for 循环而不定义外部函数,您可以使用 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;
// ...
}
i
、j
、k
等为当前迭代数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);
}
}