检查数组中的所有布尔值是否为真? [英] Check if all boolean values in an array is true?

查看:62
本文介绍了检查数组中的所有布尔值是否为真?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个布尔数组:

Let's say I have this boolean array:

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

现在,有没有一种简单的方法可以立即检查此数组中的所有值是否为true/false?

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

与其像这样做:

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

推荐答案

使用 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";
}

这篇关于检查数组中的所有布尔值是否为真?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆