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).
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.