-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只會讓你脫離最直接內部的最內層循環;它不會讓你擺脫多層循環。 –