std::vector<bool>
is specialized for space-efficiency. It doesn't provide data()
function.
You cannot address its elements because each of them is of 1 bit size, and there is nothing on machine like pointer to single bit.
The manner in which std::vector is made space efficient (as well
as whether it is optimized at all) is implementation defined. One
potential optimization involves coalescing vector elements such that
each element occupies a single bit instead of sizeof(bool) bytes.
You can access its elements using
std::vector<bool>::reference
This embedded class is the type returned by members of non-const
vector when directly accessing its elements. It accesses
individual bits with an interface that emulates a reference to a bool.
Normally data()
returns a T*
which here would be bool*
but IS NOT because the reference
and pointer
types are not really references and pointers to bool
. For example on my implementation bit type is a typedef
for unsigned long
typedef unsigned long _Bit_type;
and this is what is used by a reference
to bits called struct _Bit_reference
. This struct is used by struct _Bit_iterator
which is an iterator to elements. You can see here what it means to dereference this iterator and get element of std::vector<bool>
on this implementation:
// typedef unsigned long _Bit_type;
// struct _Bit_reference
// {
// _Bit_type * _M_p;
// ...
// };
// typedef _Bit_reference reference;
reference
_Bit_iterator::operator*() const
{ return reference(_M_p, 1UL << _M_offset); }