Check whether std::vector<bool> is comprised of only true values

一笑奈何 提交于 2019-12-12 17:09:02

问题


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).


回答1:


Given const vector<bool> foo(13) use find:

cout << (find(foo.begin(), foo.end(), false) == foo.end()) << endl;

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

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

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;

Live Examples



来源:https://stackoverflow.com/questions/37842178/check-whether-stdvectorbool-is-comprised-of-only-true-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!