C++ 我可以使用 break 退出多个嵌套的“for”循环吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1257744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I use break to exit multiple nested 'for' loops?
提问by Faken
Is it possible to use the break
function to exit several nested for
loops?
是否可以使用该break
函数退出多个嵌套for
循环?
If so, how would you go about doing this? Can you also control how many loops the break
exits?
如果是这样,你会怎么做?您还可以控制break
退出的循环次数吗?
采纳答案by Cullen Walsh
AFAIK, C++ doesn't support naming loops, like Java and other languages do. You can use a goto, or create a flag value that you use. At the end of each loop check the flag value. If it is set to true, then you can break out of that iteration.
AFAIK,C++ 不支持命名循环,就像 Java 和其他语言一样。您可以使用转到,或创建您使用的标志值。在每个循环结束时检查标志值。如果它被设置为 true,那么你可以跳出那个迭代。
回答by Henk Holterman
No, don't spoil it with a break
. This is the last remaining stronghold for the use of goto
.
不,不要用break
. 这是最后一个可供使用的据点goto
。
回答by Predelnik
Just to add an explicit answer using lambdas:
只是为了使用 lambdas 添加一个明确的答案:
for (int i = 0; i < n1; ++i) {
[&] {
for (int j = 0; j < n2; ++j) {
for (int k = 0; k < n3; ++k) {
return; // yay we're breaking out of 2 loops here
}
}
}();
}
Of course this pattern has a certain limitations and obviously C++11 only but I think it's quite useful.
当然,这种模式有一定的局限性,显然只适用于 C++11,但我认为它非常有用。
回答by Greg Hewgill
Another approach to breaking out of a nested loop is to factor out both loops into a separate function, and return
from that function when you want to exit.
打破嵌套循环的另一种方法是将两个循环分解为一个单独的函数,并return
在您想要退出时从该函数中分解出。
Of course, this brings up the other argument of whether you should ever explicitly return
from a function anywhere other than at the end.
当然,这带来了另一个论点,即您是否应该return
在函数末尾以外的任何地方显式地调用它。
回答by Karl Voigtland
breakwill exit only the innermost loop containing it.
break将只退出包含它的最里面的循环。
You can use gototo break out of any number of loops.
您可以使用goto跳出任意数量的循环。
Of course gotois often Considered Harmful.
当然goto通常被认为是有害的。
is it proper to use the break function[...]?
使用 break 函数 [...] 是否合适?
Using break and goto can make it more difficult to reason about the correctness of a program. See here for a discussion on this: Dijkstra was not insane.
使用 break 和 goto 会使推断程序的正确性变得更加困难。有关此问题的讨论,请参见此处: Dijkstra 并不疯狂。
回答by scigor
Although this answear was already presented, i think a good approach is to do the following:
虽然已经提出了这个 answear,但我认为一个好的方法是执行以下操作:
for(unsigned int z = 0; z < z_max; z++)
{
bool gotoMainLoop = false;
for(unsigned int y = 0; y < y_max && !gotoMainLoop; y++)
{
for(unsigned int x = 0; x < x_max && !gotoMainLoop; x++)
{
//do your stuff
if(condition)
gotoMainLoop = true;
}
}
}
回答by jebeaudet
How about this?
这个怎么样?
for(unsigned int i=0; i < 50; i++)
{
for(unsigned int j=0; j < 50; j++)
{
for(unsigned int k=0; k < 50; k++)
{
//Some statement
if (condition)
{
j=50;
k=50;
}
}
}
}
回答by Helio Santos
A code example using goto
and a label to break out of a nested loop:
使用goto
和标签跳出嵌套循环的代码示例:
for (;;)
for (;;)
goto theEnd;
theEnd:
回答by Deqing
One nice way to break out of several nested loops is to refactor your code into a function:
打破多个嵌套循环的一种好方法是将您的代码重构为一个函数:
void foo()
{
for(unsigned int i=0; i < 50; i++)
{
for(unsigned int j=0; j < 50; j++)
{
for(unsigned int k=0; k < 50; k++)
{
// If condition is true
return;
}
}
}
}
回答by Azeemali Hashmani
goto can be very helpful for breaking nested loops
goto 对于打破嵌套循环非常有帮助
for (i = 0; i < 1000; i++) {
for (j = 0; j < 1000; j++) {
for (k = 0; k < 1000; k++) {
for (l = 0; l < 1000; l++){
....
if (condition)
goto break_me_here;
....
}
}
}
}
break_me_here:
// Statements to be executed after code breaks at if condition