bit_vector

Category: containers

Component type: type

Description

A bit_vector is essentially a
vector<bool>: it is a
Sequence that has the same interface as
vector. The main difference is
that bit_vector is optimized for space efficiency. A
vector always requires at least one byte per element, but a
bit_vector only requires one bit per element.

Warning: The name bit_vector will be removed in a future release
of the STL. The only reason that bit_vector is a separate class,
instead of a template specialization of vector<bool>, is that this
would require partial specialization of templates. On compilers that
support partial specialization, bit_vector is a specialization of
vector<bool>. The name bit_vector is a typedef. This typedef
is not defined in the C++ standard, and is retained only for backward
compatibility.

New members

A proxy class that acts as a reference to a single bit; the reason it
exists is to allow expressions like V[0] = true. (A proxy
class like this is necessary, because the C++ memory model does not
include independent addressing of objects smaller than one byte.)
The public member functions of reference are operator bool() const,
reference& operator=(bool), and void flip(). That is, reference
acts like an ordinary reference: you can convert a reference to bool,
assign a bool value through a reference, or flip the bit that a
reference refers to.

size_type capacity() const

Number of bits for which memory has been allocated.
capacity() is always greater than or equal to size().
[2][3]

void reserve(size_type n)

If n is less than or equal to capacity(), this call has no effect.
Otherwise, it is a request for the allocation of additional
memory. If the request is successful, then capacity() is
greater than or equal to n; otherwise, capacity() is unchanged.
In either case, size() is unchanged. [2][4]

void swap(bit_vector::reference x,
bit_vector::reference y)

Swaps the bits referred to by x and y. This is a global function,
not a member function. It is necessary because the ordinary version
of swap takes arguments of type T&, and bit_vector::reference
is a class, not a built-in C++ reference.

Notes

[1]
This member function relies on member template functions, which
at present (early 1998) are not supported by all compilers. If your
compiler supports member templates, you can call this function with
any type of input iterator. If your
compiler does not yet support member templates, though, then the
arguments must either be of type const bool* or of type
bit_vector::const_iterator.

[2]
Memory will be reallocated automatically if more than capacity() -
size() bits are inserted into the bit_vector. Reallocation does
not change size(), nor does it change the values of any bits
of the bit_vector. It does, however, increase capacity(),
and it invalidates [5] any iterators that point into
the bit_vector.

[3]
When it is necessary to increase capacity(),
bit_vector usually increases it by a factor of two. It is
crucial that the amount of growth is proportional to the current
capacity(), rather than a fixed constant: in the former case
inserting a series of bits into a bit_vector is a linear time
operation, and in the latter case it is quadratic.

[4]reserve() is used to cause a reallocation manually. The main
reason for using reserve() is efficiency: if you know the
capacity to which your bit_vector must eventually grow, then
it is probably more efficient to allocate that memory all at once
rather than relying on the automatic reallocation scheme. The other
reason for using reserve() is to control the invalidation of
iterators. [5]

[5]
A bit_vector's iterators are invalidated when its memory is
reallocated. Additionally, inserting or deleting a bit in the middle
of a bit_vector invalidates all iterators that point to bits following
the insertion or deletion point. It follows that you can prevent a
bit_vector's iterators from being invalidated if you use
reserve() to preallocate as much storage as the bit_vector
will ever use, and if all insertions and deletions are at the
bit_vector's end.