简体   繁体   English

矢量怎么样? <bool> 处理引用和迭代器?

[英]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. 因为我们都知道C ++ 98 vector<bool> specialization将布尔值存储为位而不是bool变量。 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>的元素是不可寻址的,因为C ++没有指针和对bit的引用,是否有解决方法,任何明显的陷阱( 我似乎都没有注意到 ),甚至尝试和这样做?

vector<bool> 's elements are addressable as any other vector 's elements eg with operator [] . vector<bool>的元素可以作为任何其他vector的元素寻址,例如使用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. 请记住,您可以使用new运算符动态分配它。

EDIT Alternative implementations might be found eg in this thread . 编辑替代实现可能在例如此线程中找到。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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