休息; C++:它实际上打破了哪个循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10587196/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:14:57  来源:igfitidea点击:

break; C++: which loop is it actually breaking

c++cloopsbreak

提问by kiriloff

simple question regarding C++ code:

关于 C++ 代码的简单问题:

for(int i=0;i<npts;i++)
{
    for(int j=i;j<2*ndim;j++)
    {
        if(funcEvals[i]<bestListEval[j])
        {
            bestListEval[j] = funcEvals[i];
            for(int k=0;k<m_ndim;k++)
                bestList[j][k] = simplex[i][k]; 
            break; 
        }
    }
}

I want to ensure that

我想确保

  • Each line of double **simplexis inserted at most once in double **bestList
  • The instance of breakhere breaks out of the second (inner) forloop.
  • 每行double **simplex最多插入一次double **bestList
  • break这里的实例跳出第二个(内部)for循环。

Is this the case?

是这种情况吗?

回答by Sergey K.

The break statement in C++ will break out of the for or switch statement in which the break is directly placed. It breaks the innermost structure (loop or switch). In this case:

C++中的break语句会跳出直接放置break的for或switch语句。它打破了最里面的结构(循环或开关)。在这种情况下:

    for(int i=0;i<npts;i++)
    {
        for(int j=i;j<2*ndim;j++)
        {
            if(funcEvals[i]<bestListEval[j])
            {
                bestListEval[j] = funcEvals[i];
                for(int k=0;k<m_ndim;k++)
                    bestList[j][k] = simplex[i][k]; 
                break; 
            }
        }
        // after the 'break' you will end up here
    }

There is no way in C++ to have break target any other loop. In order to break out of parent loops you need to use some other independent mechanism like triggering the end condition.

在 C++ 中没有办法让 break 目标是任何其他循环。为了打破父循环,您需要使用其他一些独立的机制,例如触发结束条件。

Also, if you want to exit more than one inner-loop you can extract that loops into a function. In C++ 11 lambdas can be used to do it in-place - so there will be no need to use goto.

此外,如果您想退出多个内部循环,您可以将该循环提取到一个函数中。在 C++ 11 中,可以使用 lambda 就地执行此操作 - 因此无需使用goto

回答by JaredPar

The breakstatement in C++ will break out of the foror switchstatement in which the breakis directly placed. In this case it will break out of the for (int j = ...loop.

breakC++ 中的语句会跳出直接放置的fororswitch语句break。在这种情况下,它将跳出for (int j = ...循环。

There is no way in C++ to have breaktarget any other loop. In order to break out of parent loops you need to use some other independent mechanism like triggering the end condition.

在 C++ 中没有办法以break任何其他循环为目标。为了打破父循环,您需要使用其他一些独立的机制,例如触发结束条件。

// Causes the next iteration of the 'for (int i ...' loop to end the loop)
i = npts;

// Ends the 'for (int j ...' loop
break;

回答by Martol1ni

You are breaking out of your second loop to your first loop.

您正在从第二个循环跳到第一个循环。

for (int i=0; i<npts; i++)

You could set a boolean at the top

您可以在顶部设置一个布尔值

bool shouldBreak = false;

and when you write break, write

当你写break时,写

shouldBreak = true;
break;

Then at the end of your loop, check each time,

然后在循环结束时,每次检查,

if (shouldBreak) break;

回答by kalpani thakshila

for (int i = 0; i < npts; i++)

You could set a boolean at the top

您可以在顶部设置一个布尔值

bool shouldBreak = false;

and when you want to break the other loop, write

当你想打破另一个循环时,写

shouldBreak = true;
break;

回答by kayleeFrye_onDeck

Using break

使用 break

breakwill break out of the whatever repeating or iterating loop that it's currently inside of. This verbose image guide is meant to highlight break's behavior, not illustrate good coding practices. The inner for-loop would have sufficed, but like I said, this is for visual purposes:

break将跳出它当前所在的任何重复或迭代循环。这个详细的图像指南旨在突出break的行为,而不是说明良好的编码实践。内部 for 循环就足够了,但就像我说的,这是出于视觉目的:

double-iterating-loops

双迭代循环

Alternative Advice

替代建议

Use Modern C++'s various search algorithms available in <algorithm>to search through containers and to a some extent, strings. The reason for this is two-fold:

使用现代 C++ 的各种搜索算法<algorithm>来搜索容器,并在一定程度上搜索字符串。这样做的原因有两个:

  1. Shorter and easier to read code
  2. Usually just as fast if not faster than anything you could have written yourself
  1. 更短、更容易阅读的代码
  2. 通常与您自己编写的任何内容一样快,如果不快的话

These code-samples will need the same boiler-plate, excepting <algorithm>for the older for-loop searches:

这些代码示例将需要相同的样板,除了<algorithm>旧的 for 循环搜索:

#include <vector>
#include <algorithm>

std::vector<int> int_container = { 10, 23, 10345, 432, 2356, 999, 1234, 0x45f };
bool has1234 = false;

The modern way would be something like this, where we quickly search through the container, and if the search-iterator isn't at the very end of the container(notthe last element), we know the value it's searching for was located.

现代方法是这样的,我们快速搜索容器,如果搜索迭代器不在容器的最末端不是最后一个元素),我们知道它正在搜索的值是定位

This code achieves the same result with less lines and less potential points-of-failure for user-written code, like the older alternatives below it.

对于用户编写的代码,此代码以更少的行数和更少的潜在故障点实现了相同的结果,就像它下面的旧替代方案一样。

Modern C++ style

现代 C++ 风格

auto search_position = std::find( int_container.begin(), int_container.end(), 1234 ) ;
if ( search_position != int_container.end() )
    has1234 = true;

C++11 range-based for-loop

C++11 基于范围的 for 循环

for ( auto i : int_container )
{
    if ( i == 1234 )
    {
        has1234 = true;
        break;
    }
}

Old-school C-style for-loop:

老式的 C 风格 for 循环:

for ( int i = 0; i < int_container.size(); i++ )
{
    if ( int_container[i] == 1234 )
    {
        has1234 = true;
        break;
    }
}