Check if all boolean values in an array is true?

Viewed 23746

Let's say I have this boolean array:

bool something[4] = {false, false, false, false};

Now, is there any simple way to check if all the values in this array is true/false at once?

Instead of doing it like this:

if(something[0] == false && something[1] == false..)
dothis();
4 Answers

You could search for the first false flag:

bool something[n];
...

bool allTrue = (std::end(something) == std::find(std::begin(something),
                                                 std::end(something),
                                                 false) );
Related