检查std :: vector< bool>是否只包含真实值 [英] Check whether std::vector<bool> is comprised of only true values

查看:78
本文介绍了检查std :: vector< bool>是否只包含真实值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定包含布尔值的向量(通常优化为位数组)是否仅包含真值的最快方法是什么?对于小型向量,我认为将向量与仅存储真值的另一个向量进行比较可能不是一个坏主意(假设我们知道两个向量的大小).

What is the fastest way to determine whether a vector holding boolean values (which is typically optimized as a bit array) only holds true values? For small vectors, I think it might not be a bad idea to just compare the vector against another vector storing only true values (assuming we know the size of both vectors).

推荐答案

给出const vector<bool> foo(13)使用或者如果您具有 none_of :

Or if you have c++11 you can none_of:

cout << none_of(cbegin(foo), cend(foo), logical_not<bool>()) << endl;

或者,如果您在编译时知道vector的大小,则可以使用 bitset 'a all 方法:

Alternatively if you know your vector's size at compile time you can use bitset'a all method:

bitset<13> foo;

cout << foo.all() << endl;

实时示例

这篇关于检查std :: vector&lt; bool&gt;是否只包含真实值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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