在 Windows 中,可以使用 C 中的结构化异常处理来编写一个伪循环,打印从 1 到 1000 的所有数字,如下所示:
int n = 0;
__try {
*(int *)0 = 0;
}
__except(printf("%i\n", ++n), n < 1000 ? -1 : 1) {
}
我想知道 C/C++ 中是否还有其他方法来创建一个循环,如果您在代码中搜索常见的可疑关键字 for
,那么该循环对于检测起来并不简单。 , while
和goto
.
最佳答案
在 C++ 中,一个简单的 lambda 就可以做到这一点:
std::function<void(int,int)> print = [&](int from, int to)
{
std::cout << from << " ";
if ( from < to ) print(++from, to);
};
print(1, 1000);
它将打印从 1
到 1000
的所有整数。并且它不使用 for
、while
或 goto
。
关于c++ - 不使用 for、while 或 goto 的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14241325/