我可以使用休息时间退出多个嵌套循环吗?
-
12-09-2019 - |
题
是否可以使用 break
功能以退出几个嵌套 for
循环?如果是这样,您将如何做到这一点?您还可以控制休息出口多少循环吗?
解决方案
Afaik,C ++不支持命名循环,例如Java和其他语言。您可以使用goto,也可以创建使用的标志值。在每个循环结束时,检查标志值。如果将其设置为True,那么您可以突破该迭代。
其他提示
不,不要用一个 break
. 。这是使用的最后剩下的据点 goto
.
从嵌套循环中突破的另一种方法是将两个循环分为一个单独的函数,并且 return
当您想退出时,该功能从该功能。
当然,这提出了您是否应该明确的另一个论点 return
从末尾以外的任何地方的功能。
只是为了使用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
}
}
}();
}
当然,这种模式有一定的局限性,显然只有C ++ 11,但我认为它非常有用。
休息 将仅退出最内在的循环。
您可以使用 去 脱离任何数量的循环。
当然 去 经常 被认为有害.
使用断裂功能[...]是否合适?
使用休息和goto可以使程序的正确性更加困难。请参阅此处的讨论: Dijkstra不是疯狂的.
尽管已经提出了这个答案,但我认为一种很好的方法是做以下操作:
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;
}
}
}
这个怎么样?
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;
}
}
}
}
使用代码示例使用 goto
和一个标签以突破嵌套循环:
for (;;)
for (;;)
goto theEnd;
theEnd:
突破几个嵌套循环的一种好方法是将代码重构为一个函数:
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;
}
}
}
}
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
这
break
语句终止执行最近的封闭do
,for
,switch
, , 或者while
它出现的声明。控制权传递到终止语句之后的陈述。
从 MSDN.
我确实认为 goto
在这种情况下是有效的:
模拟一个 break
/continue
, ,您想要:
休息
for ( ; ; ) {
for ( ; ; ) {
/*Code here*/
if (condition) {
goto theEnd;
}
}
}
theEnd:
继续
for ( ; ; ) {
for ( ; ; ) {
/*Code here*/
if (condition) {
i++;
goto multiCont;
}
}
multiCont:
}
诸如PHP之类的其他语言接受休息的参数(IE Break 2;),以指定要突破的嵌套循环级别的数量,但是C ++却没有。您将必须通过在循环之前设置为false的布尔来解决它,如果要断裂,请在循环中设置为true,以及嵌套循环后的有条件休息,检查布尔值是否设置为true如果是,请休息一下。
我知道这是旧帖子。但是我建议一些合乎逻辑,更简单的答案。
for(unsigned int i=0; i < 50; i++)
{
for(unsigned int j=0; j < conditionj; j++)
{
for(unsigned int k=0; k< conditionk ; k++)
{
// If condition is true
j= conditionj;
break;
}
}
}
仅将任何数量的循环折断一个 bool
变量见:
bool check = true;
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)
{
check = false;
break;
}
}
if (!check)
{
break;
}
}
if (!check)
{
break;
}
}
在此代码中,我们 break;
所有的循环。
while (i<n) {
bool shouldBreakOuter = false;
for (int j=i + 1; j<n; ++j) {
if (someCondition) {
shouldBreakOuter = true;
}
}
if (shouldBreakOuter == true)
break;
}
您可以使用尝试...捕获。
try {
for(int i=0; i<10; ++i) {
for(int j=0; j<10; ++j) {
if(i*j == 42)
throw 0; // this is something like "break 2"
}
}
}
catch(int e) {} // just do nothing
// just continue with other code
如果您必须一次突破几个循环,那么无论如何,这通常是一个例外。
从循环中脱颖而出对我来说有点奇怪,因为循环的语义通常表明它将执行指定的次数。但是,在所有情况下都不错。如果您要在集合中搜索某些东西,并且想在找到它后打破,则很有用。但是,在C ++中不可能突破嵌套环。它是通过使用标记的休息时间在其他语言中。您可以使用标签和goto,但这可能会在晚上给您胃灼热。似乎是最好的选择。