Is it possible to use the break
function to exit several nested for
loops? If so, how would you go about doing this? Can you also control how many loops the break exits?
相关问题
- Is there a limit to how many levels you can nest i
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Just to add an explicit answer using lambdas:
Of course this pattern has a certain limitations and obviously C++11 only but I think it's quite useful.
You can use try...catch.
If you have to break out of several loops at once, it is often an exception anyways.
goto can be very helpful for breaking nested loops
How about this?
Breaking out of a for-loop is a little strange to me, since the semantics of a for-loop typically indicate that it will execute a specified number of times. However, it's not bad in all cases; if you're searching for something in a collection and want to break after you find it, it's useful. Breaking out of nested loops, however, isn't possible in C++; it is in other languages through the use of a labeled break. You can use a label and a goto, but that might give you heartburn at night..? Seems like the best option though.