1. 程式人生 > >c++ 判斷陣列元素是否都是奇數(all_of)

c++ 判斷陣列元素是否都是奇數(all_of)

 

#include <iostream>     // std::cout
#include <algorithm>    // std::all_of
#include <array>        // std::array
using namespace std;
int main () {
    array<int,8> foo = {3,4,7,11,13,17,19,23};
    
    if ( all_of(foo.begin(), foo.end(), [](int i){return i%2;}) )
        cout << "All the elements are odd numbers.\n";
    else{
        cout<<"not all are odd\n";
    }
    return 0;
}