Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 630
iKlsR Avatar asked Oct 21 '22 20:10

iKlsR


1 Answers

vector<bool>'s elements are addressable as any other vector's elements e.g. 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 e.g. in this thread.

like image 83
Boris Strandjev Avatar answered Oct 31 '22 20:10

Boris Strandjev