-3
在do-while循环中我正在使用for循环读取矢量的值。这个向量有重复的值。我想停止使用if语句的for循环。 如何用if语句在C++中的do-while循环中终止for循环?
do {
/* some statement1*/
for(int i = 0; i < n; ++i)
{
if (vec[i] == a)
{
/* some statement2*/
}
} //here i want to terminate the for loop once it finds value a in vector
} while (statement3);
你可以使用lambda,并立即调用它,然后用'return'作为'break'。 –
看起来你错过了一些作品。请提供[最小,**完整**和可验证示例](https://stackoverflow.com/help/mcve)。 – InternetAussie
我认为你误解了break关键字的作用 - break只会让你脱离最直接内部的最内层循环;它不会让你摆脱多层循环。 –