简体   繁体   中英

How does vector<bool> deal with references and iterators?

As we all probably know the C++ 98 vector<bool> specialization stores boolean values as bits rather than as bool variables. vector<bool> 's elements aren't addressable because C++ doesn't have pointers and references to bits, is there a workaround to this, any obvious pitfalls ( that i seem to be oblivious to ) and is it practical to even try and do so?

vector<bool> 's elements are addressable as any other vector 's elements eg with operator [] . However, the operations will be slower, because of the memory compression.

Maybe faster implementation will use your own inmemory implementation and use binary shifts to address specific boolean value.

Also an alternative will be to use simple array in places where this is appropriate. Remember that you can allocate it dynamically using the new operator.

EDIT Alternative implementations might be found eg in this thread .

vector<bool>使用带有重载运算符的包装器对象,而不是引用/指向位的指针,这些运算符的行为(在大多数情况下)类似于布尔值的引用/指针。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM