在 c++ 中创建递归 for 语句
Create a recursive for statement in c++
我想知道是否真的可以在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;
// ...
}
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);
}
}
相关文章:
- 如何在 FOR 语句中间阻止 Visual Studio 2017 c++ 自动完成)?
- C++:关于for语句和参数的一般问题
- 这个基于范围的'for'语句需要一个合适的函数,但找不到任何函数
- python 和 c++ 的结果对于嵌套的 for 语句是不同的
- 在 c++ 中创建递归 for 语句
- 如何查找用户使用 do/while、While 语句或 For 语句输入的几个整数的总和
- for 语句内的动态数组
- 为什么基于范围的 for 语句通过 auto&& 获取范围?
- ranged-for 语句中的表达式是否在每次迭代时计算
- 即使我设置了限制,我的 for 语句也会不断重复
- Visual Studio 2015正在缩进for语句的多行
- 在for语句参数内部的C/C++算术运算中
- 获取 while、if 和 for 语句的用户定义类的对象值
- sqlite3_prepare_v2 返回 null for 语句
- 基于范围的 for 语句中的 GCC 错误
- for 语句超出范围错误
- 计算没有If/While/For语句或任何数据结构的更改
- C++11:基于范围的 for 语句:"range-init"生命周期?
- 为什么对“for”语句范围有特殊规则
- 使用for语句计算int输入的和