打破C++;:它实际上打破了哪个循环 关于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; } } } >(int i=0;i < p> < C++ >代码>中断>代码>将在 > >或代码>开关>代码>语句中,其中直接放置中断>代码>。 < C++ >没有代码> >破坏者>代码>目标任何其他循环。为了打破父循环,你需要使用其他独立的机制,比如触发结束条件。 // Causes the next iteration of the 'for (int i ...' loop to end the loop) i = npts; // Ends the 'for (int j ...' loop break; C++中的中断语句将从中断直接放置的for或Switt语句中分离出来,它打破最内部的结构(循环或开关)。 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 } for(inti=0;i

打破C++;:它实际上打破了哪个循环 关于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; } } } >(int i=0;i < p> < C++ >代码>中断>代码>将在 > >或代码>开关>代码>语句中,其中直接放置中断>代码>。 < C++ >没有代码> >破坏者>代码>目标任何其他循环。为了打破父循环,你需要使用其他独立的机制,比如触发结束条件。 // Causes the next iteration of the 'for (int i ...' loop to end the loop) i = npts; // Ends the 'for (int j ...' loop break; C++中的中断语句将从中断直接放置的for或Switt语句中分离出来,它打破最内部的结构(循环或开关)。 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 } for(inti=0;i,c++,c,loops,break,C++,C,Loops,Break,您正在从第二个循环切换到第一个循环 for (int i=0; i<npts; i++) 当你写“休息”时,写“休息” shouldBreak = true; break; 然后在循环结束时,每次检查 if (shouldBreak) break; 当你想打破另一个循环时,写 shouldBreak = true; break; 使用break break将打破它当前所在的任何重复或迭代循环。本详细的图像指南旨在强调break的行为,而不是说明良好的编码实践。内部for循环已经足够

您正在从第二个循环切换到第一个循环

for (int i=0; i<npts; i++)
当你写“休息”时,写“休息”

shouldBreak = true;
break;
然后在循环结束时,每次检查

if (shouldBreak) break;
当你想打破另一个循环时,写

shouldBreak = true;
break;
使用
break
break
将打破它当前所在的任何重复或迭代循环。本详细的图像指南旨在强调
break
的行为,而不是说明良好的编码实践。内部for循环已经足够了,但正如我所说,这是为了视觉目的:

替代建议 使用现代C++在
中提供的各种搜索算法来搜索容器,并在某种程度上搜索字符串。原因有两个:

  • 代码更短,更易于阅读
  • 通常和你自己写的东西一样快
  • 这些代码示例将需要相同的锅炉板,但较旧的for循环搜索除外:

    #include <vector>
    #include <algorithm>
    
    std::vector<int> int_container = { 10, 23, 10345, 432, 2356, 999, 1234, 0x45f };
    bool has1234 = false;
    
    C++11基于范围的for循环 老派C型for loop:
    for(int i=0;i
    是的,它是从第二个
    for
    循环中断。
    中断
    总是从代码中该点处激活的最内部结构(循环或
    开关
    )中断(在这种情况下,
    for(j…
    循环).Break总是打断最里面的循环。如果你想跳出更多的循环,你需要有一个bool或什么东西来跟踪何时继续或不继续。如果
    funcEvals[i]添加了“C”,则
    Break
    语句总是会出现标记,因为问题与C相关,代码是用C编写的。标签和
    goto
    通常可用于中断其他循环。我通常认为,将此类内容放入一个名为
    FindFirstTelethXYZ
    的函数中,然后在找到它时返回最方便。不要忘记goto、异常和longjmp:)设置
    i=npts
    来中断循环是令人困惑的,请改用布尔值。@MooingDuck我的示例旨在对OP的样本进行最小程度的侵入。
    bool shouldBreak = false;
    
    shouldBreak = true;
    break;
    
    #include <vector>
    #include <algorithm>
    
    std::vector<int> int_container = { 10, 23, 10345, 432, 2356, 999, 1234, 0x45f };
    bool has1234 = false;
    
    auto search_position = std::find( int_container.begin(), int_container.end(), 1234 ) ;
    if ( search_position != int_container.end() )
        has1234 = true;
    
    for ( auto i : int_container )
    {
        if ( i == 1234 )
        {
            has1234 = true;
            break;
        }
    }
    
    for ( int i = 0; i < int_container.size(); i++ )
    {
        if ( int_container[i] == 1234 )
        {
            has1234 = true;
            break;
        }
    }