Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 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.
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.
1
1000