6

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();
Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
Hummelmannen
  • 133
  • 1
  • 1
  • 8

4 Answers4

22

Use std::all_of

#include<algorithm>
...
if (std::all_of(
      std::begin(something), 
      std::end(something), 
      [](bool i)
            { 
              return i; // or return !i ;
            }
)) {
      std::cout << "All numbers are true\n";
}
alfC
  • 14,261
  • 4
  • 67
  • 118
P0W
  • 46,614
  • 9
  • 72
  • 119
  • @Salgar Yeah, they are. `bool` is an integral type -> it's a number. –  Dec 19 '13 at 17:09
  • I understand that, he edited his answer. It was strange before. – Salgar Dec 19 '13 at 17:14
  • If I'm using the std namespace I don't have to type std:: before things like cout and such, right? Does that apply to this as well? Also, if someone would like to simply explain why not that many people are using namespace std I'd appreciate that. :) – Hummelmannen Dec 19 '13 at 17:14
  • 3
    http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Salgar Dec 19 '13 at 17:16
  • @Hummelmannen yes it does apply to this as well, and refer the link that Salgar commented above – P0W Dec 19 '13 at 17:17
  • @Hummelmannen Not many people use `using namespace std;` because it's evil. –  Dec 19 '13 at 17:55
  • 3
    It would be nice to remark that all_of is only defined from C++11 on... – Roman Rdgz Jul 02 '15 at 08:38
  • 1
    Just wanted to add an approach with pointers: `std::all_of(something, &something[4], [](bool b) {return b;});` – Vinz Nov 03 '16 at 20:30
8

You can do this by summing:

#include <numeric> 

int sum = std::accumulate(bool_array, bool_array + 4, 0);
if(sum == 4) /* all true */;
if(sum == 0) /* all false */;

This has the advantage of finding both conditions in one pass, unlike the solution with all_of which would require two.

Eric
  • 95,302
  • 53
  • 242
  • 374
  • 4
    What about any array with millions bools and first one as `false` and need to check all `true` ? `std::all_of` just uses `std::find_if_not` – P0W Dec 19 '13 at 17:23
  • @P0W: I was assuming the question was to find _both_ conditions, not either condition – Eric Dec 19 '13 at 17:34
  • 3
    Still you should not need to go through the whole array for either of the conditions if the first two elements are true and false respectively.. – Moberg Sep 11 '17 at 09:37
6

Use a for loop.

allTrue = true;
allFalse = true;
for(int i=0;i<something.size();i++){
    if(something[i]) //a value is true
        allFalse = false; //not all values in array are false
    else //a value is false
        allTrue = false; //not all values in array are true
}

My syntax might be a bit off (haven't used C++ in a while) but this is the general pseudocode.

Embattled Swag
  • 1,479
  • 12
  • 23
6

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) );
Den-Jason
  • 2,395
  • 1
  • 22
  • 17