Name

vector<bool> class — Specialized vector of bool

Synopsis

template <typename Alloc>
class vector<bool, Alloc> {
public:
  typedef bool const_reference;
  typedef  . . .  iterator;
  typedef  . . .  const_iterator;
  typedef  . . .  size_type;
  typedef  . . .  difference_type;
  typedef bool value_type;
  typedef Alloc allocator_type;
  typedef  . . .  pointer;
  typedef  . . .  const_pointer  typedef std::reverse_iterator<iterator> reverse_iterator;
  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
   
  class reference;
  static void swap(reference x, reference y);
  void flip(  );
   . . .  // Same as vector<>  . . . 
};

The vector<bool> specialization is an interesting beast. It is an attempt to demonstrate how to define a container that uses a proxy to represent the elements of the container. The bool elements are packed into integers, and the vector<bool>::reference type is a proxy that represents a single bool element by keeping track of the bit number within the integer and the integer’s index in the vector.

However, by using a proxy, vector<bool> violates the constraints of a container, so it cannot be used in many situations that call for a standard container. In particular, the pointer type cannot point to an element of the container because C++ does not have a type that can point to a single bit. Many algorithms require the pointer type, and so they cannot work with a vector<bool> object.

If you need to use a compact, fixed-size set of bits, use the bitset class template. If you need a standard ...

Get C++ In a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.