C++ 如何检查数组的所有元素是否都大于零

Chr*_*ser -6 c++ arrays loops c++11

我对 C++ 相当陌生,我正在尝试创建一个需要像这样进行检查的循环:

“如果数组中的所有元素都大于零,则中断循环,如果不大于零,则从头开始重新循环”

请你帮助我,因为我不知道如何开始

多谢你们

P0W*_*P0W 5

使用std::all_of, 或姐妹无论你有什么逻辑我不知道它的正确性

while ( !std::all_of( std::begin(arr), std::end(arr), [](int i){ return i > 0; }) )
{
  /* if all the elements in the array are greater than zero, 
     break the loop, 
     if they are not then restart the loop from the beginning
  */

}
Run Code Online (Sandbox Code Playgroud)