C++ Reference ยท Containers
C++ std::vector<bool>
Reference for std::vector<bool>: the bit-packed specialization, the proxy reference that breaks auto and references, and the alternatives to use instead.
std::vector<bool> is a specialization of std::vector that the standard allows (and, in practice, requires) to pack the elements into bits, one bit per bool. That saves memory, about eight times compared with std::vector<char>, but it means the container no longer behaves like the other vectors. It does not store bool objects, so it cannot give out a bool&, and data() does not exist.
Instead, operator[] returns a small proxy object (std::vector<bool>::reference) that reads and writes a single bit. The proxy works for most everyday code but breaks generic code that assumes T&. Most style guides call vector<bool> a historical mistake and suggest alternatives.
std::vector<bool> flags(8, false); flags[3] = true; // the proxy sets bit 3
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template <class Allocator> class vector<bool, Allocator>; class std::vector<bool>::reference { public: operator bool() const noexcept; reference& operator=(bool) noexcept; reference& operator=(const reference&) noexcept; void flip() noexcept; }; void flip() noexcept; // invert all bits static void swap(reference a, reference b) noexcept;
| Name | Description |
|---|---|
reference |
Proxy for one bit. Converts to bool; assigning to it writes the bit. |
flip() (vector) |
Inverts every bit in the container. |
flip() (reference) |
Inverts that one bit. |
swap(a, b) |
Static function that swaps the values of two elements. |
Returns: operator[], at, front and back return reference (const versions return bool).
Errors / undefined behavior: the same preconditions as vector<T> (operator[] out of range is undefined; at() throws std::out_of_range). A proxy that outlives its vector, or one invalidated by reallocation, is dangling.
Usage notes
- No
data(), noT&, and&v[0]is not abool*. Code that passes a vector's buffer to C functions cannot use it. autodeduces the proxy:auto x = v[0];gives areferencethat tracks the vector, not aboolcopy. Writebool x = v[0];orauto x = static_cast<bool>(v[0]);.- Thread safety: two threads writing different elements of an ordinary vector is safe, but writing different
vector<bool>elements can race, because both touch the same word. - Iterators are not references-based: they are not true forward iterators in the strict sense, and algorithms that require
bool&(such asstd::ranges::sort) can fail to compile. - Compact: a million flags takes about 125 KB, versus 1 MB for
vector<char>. That is the one reason to keep it. - Alternatives:
std::vector<char>orstd::vector<std::uint8_t>for speed and real references,std::bitset<N>when the size is a compile-time constant,boost::dynamic_bitsetfor a runtime-sized bit set, or a wrapper struct with abool. - Templates: in generic code,
std::vector<T>withT = boolinstantiates this specialization; static-assert against it or special-case it if you needT&. std::vector<bool>is still a valid container, sosize(),push_back,resize,eraseand iteration work as usual.
Related
- std::vector, std::bitset, std::array
- Lessons: Bitset, std::vector in depth