-3

forが他の for の中にある場合を考えてみましょう

int f( ... )
{
  for (int i = start_a; i < end_a; i++)
  {
    for (int j = start_b; j < end_b; j++)
    {
      // make some computation
      if( i_must_exit == true)
      {
        // exit from all for
      }
    }
  }

  // I want arrive here
}

for両方のループから抜け出したいのです。内部関数を除外したり、例外をスローしたりしない限り、これは C++03 では簡単ではありません。C++11 がこれを行うためのメカニズムを導入したかどうか疑問に思っていました。

4

3 に答える 3

11

I think the best solution is to use iterators and algorithms, like std::find_if.

于 2013-04-26T14:25:34.513 に答える
2

最良の解決策は、ラムダを使用することだと思います...次のようなものです:

int f()
{
  [&]{
    for (int i = start; i < end; i++)
    {
      for (int j = start_; j < end_; j++)
      {
        // make some computation
        if( i_must_exit == true)
        {
          // exit from all for
          return;
        }
      }
    }
  }(); // execute this code now!

  // continue with computation
}
于 2013-04-26T14:23:49.750 に答える