【问题标题】:Breaking out of nested loops in C++ [duplicate]打破C ++中的嵌套循环[重复]
【发布时间】:2014-11-11 18:53:54
【问题描述】:

假设我们有以下代码,当 break 时,我们想跳出内循环和外循环,而不仅仅是内循环,直接进入 blablabla。我们如何在 C++ 中做到这一点?

for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
         if (some condition) {
             // Do something and break...
             break; // Breaks out of the inner loop
         }
    }
}

blablabla
...

【问题讨论】:

  • 我的异端答案是 goto。现在用你的火把和叉子杀了我;-)
  • 你能举一个更具体的例子吗?我从未见过您想要跳出多个循环的情况,至少不是在正确编写的代码中。我看到的大多数示例都是尝试在单个函数中做太多事情的结果。

标签: c++ for-loop


【解决方案1】:

“直接转到 [...]”对于 goto 来说是个完美的工作。它是!

for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
         if (some condition) {
             // Do something and break...
             goto afterLoop; // Breaks out of both loops
         }
    }
}
afterLoop:
// More stuff

【讨论】:

  • 为什么我们要添加不必要的分支和复杂的循环条件,当我们有一个工具可以用最少的工作完成我们所需要的?这是做到这一点的最佳方式。效率高,代码的用途和执行方式一目了然。
  • @Serge 所有的常识不会阻止所有goto 的批评者。教条。
【解决方案2】:

有两种方法可以解决:

解决方案 1:使用布尔条件而不是第二个 for 循环

for (int i = 0; i < m; i++) {
int j = 0;
bool conditionOut=false;
    while (!conditionOut && j<n)
    {
       if (some condition)
       {
          conditionOut =true;
          break;
       }
    }
}

解决方案 2:使用 goto(Stroustrup 提到它是 goto 在 C++ 中有用的少数示例之一)

【讨论】:

    【解决方案3】:

    你可以在外部循环中构建一个变量检查​​,比如说:

    bool end;
    

    在内部循环中,在某个时刻,您想要突破并将 end 设置为 true。 你还需要一个 if 条件。

    【讨论】:

      【解决方案4】:

      您可以为此目的使用“goto”。

      【讨论】:

        【解决方案5】:

        例如

        for ( int i = 0; i < m; i++ ) 
        {
            bool exit = false;
        
            for ( int j = 0; !exit && j < n; j++ ) 
            {
                 if ( ( exit = some condition ) ) 
                 {
                     // Do something and break...
                 }
            }
        
            if ( exit ) break;
        }
        

        或者

        bool exit = false;
        
        for ( int i = 0; !exit && i < m; i++ ) 
        {
            for ( int j = 0; !exit && j < n; j++ ) 
            {
                 if ( ( exit = some condition ) ) 
                 {
                     // Do something and break...
                 }
            }
        }
        

        【讨论】:

          【解决方案6】:

          使用另一个 int 变量。

          for (int i = 0, Out = 0; i < m && Out == 0; i++) {
              for (int j = 0; j < n && Out == 0; j++) {
                   if (some condition) {
                       Out = 1;
                   }
              }
          }
          

          【讨论】:

            【解决方案7】:

            您可以将循环提取到一个单独的函数中并使用return

            void doTheLoop(int n, int m) {
                for (int i = 0; i < m; i++) {
                    for (int j = 0; j < n; j++) {
                         if (some condition) {
                             // Do something and break...
                             return;
                         }
                    }
                }
            }
            
            doTheLoop(n, m);
            //blablabla ...
            

            【讨论】:

              猜你喜欢
              • 2015-03-03
              • 2010-10-13
              • 1970-01-01
              • 1970-01-01
              • 2013-03-09
              • 2012-11-30
              相关资源
              最近更新 更多