Simultaneous fulfilment of conditions for all elements of the mass



  • How can all parts of the mass be circumvent the conditions of the operator? One condition must be met at the same time for all elements of the mass, for example, if all elements of the mass are less than 5 j++♪ I wish there was something in that way in the sense of:

    if(for(int i=0; i<10; i++) arr[i]<5) j++;
    


  • We can do this:

    bool flag = true;
    for (int i = 0; i < 10; ++i)
        if (arr[i] >= 5) {
            flag = false;
            break;
        }
    if (flag) {
        // Все элементы меньше 5
    }
    

    Or so:

    bool cond(int* arr, size_t N)
    {
        for (size_t i = 0; i < N; ++i)
            if (arr[i] >= 5)
                 return false;
        return true;
    }
    

    if (cond(arr, 10)) {
    // Все элементы меньше 5
    }

    Lambdas may also be used (but the code is less understandable):

    if (&arr {
    for (size_t i = 0; i < 10; ++i)
    if (arr[i] >= 5)
    return false;
    return true;
    }())
    {
    // Все элементы меньше 5
    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2