# std::vector<bool>

> C++ Atlas: `cppatlas.com/w/cpp/container/vector_bool` | Source: https://en.cppreference.com/w/cpp/container/vector_bool | Domain: Containers | Standards: C++11, C++17, C++20, C++23

std::vector<bool> is a possibly space-efficient specialization of std::vector for the type bool.

## Official Specification Summary (cppreference)

```cpp
// Defined in header <vector>

template<
    class Allocator
> class vector<bool, Allocator>;
```

`std::vector<bool>` is a possibly space-efficient specialization of `std::vector` for the type `bool`.

The manner in which `std::vector<bool>` 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.

`std::vector<bool>` behaves similarly to `std::vector`, but in order to be space efficient, it:

- Does not necessarily store its elements as a contiguous array.
- Exposes class `std::vector<bool>::reference` as a method of accessing individual bits. In particular, objects of this class are returned by `operator[]` by value.
- Does not use `std::allocator_traits::construct` to construct bit values.
- Does not guarantee that different elements in the same container can be modified concurrently by different threads.

### Member types

**Member type — Definition**

- `value_type`: `bool`
- `allocator_type`: `Allocator`
- `size_type`: implementation-defined
- `difference_type`: implementation-defined
- `reference`: proxy class representing a reference to a single `bool` (class)
- `const_reference`: `bool`
- `pointer`: implementation-defined
- `const_pointer`: implementation-defined
- `iterator`: implementation-defined (until C++20) implementation-defined *ConstexprIterator* (since C++20)
- `const_iterator`: implementation-defined (until C++20) implementation-defined *ConstexprIterator* (since C++20)
- `reverse_iterator`: `std::reverse_iterator<iterator>`
- `const_reverse_iterator`: `std::reverse_iterator<const_iterator>`

### Member functions

- (constructor): constructs the `vector` (public member function of `std::vector<T,Allocator>`)
- (destructor): destructs the `vector` (public member function of `std::vector<T,Allocator>`)
- `operator=`: assigns values to the container (public member function of `std::vector<T,Allocator>`)
- `assign`: assigns values to the container (public member function of `std::vector<T,Allocator>`)
- `assign_range` (C++23): assigns a range of values to the container (public member function of `std::vector<T,Allocator>`)
- `get_allocator`: returns the associated allocator (public member function of `std::vector<T,Allocator>`)

**Element access**

- `at`: access specified element with bounds checking (public member function of `std::vector<T,Allocator>`)
- `operator[]`: access specified element (public member function of `std::vector<T,Allocator>`)
- `front`: access the first element (public member function of `std::vector<T,Allocator>`)
- `back`: access the last element (public member function of `std::vector<T,Allocator>`)

**Iterators**

- `begin`, `cbegin` (C++11): returns an iterator to the beginning (public member function of `std::vector<T,Allocator>`)
- `end`, `cend` (C++11): returns an iterator to the end (public member function of `std::vector<T,Allocator>`)
- `rbegin`, `crbegin` (C++11): returns a reverse iterator to the beginning (public member function of `std::vector<T,Allocator>`)
- `rend`, `crend` (C++11): returns a reverse iterator to the end (public member function of `std::vector<T,Allocator>`)

**Capacity**

- `empty`: checks whether the container is empty (public member function of `std::vector<T,Allocator>`)
- `size`: returns the number of elements (public member function of `std::vector<T,Allocator>`)
- `max_size`: returns the maximum possible number of elements (public member function of `std::vector<T,Allocator>`)
- `reserve`: reserves storage (public member function of `std::vector<T,Allocator>`)
- `capacity`: returns the number of elements that can be held in currently allocated storage (public member function of `std::vector<T,Allocator>`)

**Modifiers**

- `clear`: clears the contents (public member function of `std::vector<T,Allocator>`)
- `insert`: inserts elements (public member function of `std::vector<T,Allocator>`)
- `insert_range` (C++23): inserts a range of elements (public member function of `std::vector<T,Allocator>`)
- `append_range` (C++23): adds a range of elements to the end (public member function of `std::vector<T,Allocator>`)
- `emplace` (C++11): constructs element in-place (public member function of `std::vector<T,Allocator>`)
- `erase`: erases elements (public member function of `std::vector<T,Allocator>`)
- `push_back`: adds an element to the end (public member function of `std::vector<T,Allocator>`)
- `emplace_back` (C++11): constructs an element in-place at the end (public member function of `std::vector<T,Allocator>`)
- `pop_back`: removes the last element (public member function of `std::vector<T,Allocator>`)
- `resize`: changes the number of elements stored (public member function of `std::vector<T,Allocator>`)
- `swap`: swaps the contents (public member function of `std::vector<T,Allocator>`)

**vector<bool> specific modifiers**

- `flip`: flips all the bits (public member function)
- `swap` [static]: swaps two `std::vector<bool>::reference`s (public static member function)

### Non-member functions

- `operator==`, `operator!=` (removed in C++20), `operator<` (removed in C++20), `operator<=` (removed in C++20), `operator>` (removed in C++20), `operator>=` (removed in C++20), `operator<=>` (C++20): lexicographically compares the values of two `vector`s (function template)
- `std::swap(std::vector)`: specializes the `std::swap` algorithm (function template)
- `erase(std::vector)` (C++20), `erase_if(std::vector)`: erases all elements satisfying specific criteria (function template)

### Helper classes

- `std::hash<std::vector<bool>>` (C++11): hash support for `std::vector<bool>` (class template specialization)

### Deduction guides (C++17)

### Notes

If the size of the bitset is known at compile time, `std::bitset` may be used, which offers a richer set of member functions. In addition, `boost::dynamic_bitset` exists as an alternative to `std::vector<bool>`.

Since its representation may be optimized, `std::vector<bool>` does not necessarily meet all *Container* or *SequenceContainer* requirements. For example, because `std::vector<bool>::iterator` is implementation-defined, it may not satisfy the *LegacyForwardIterator* requirement. Use of algorithms such as `std::search` that require *LegacyForwardIterator*s may result in either compile-time or run-time errors.

The Boost.Container version of `vector` does not specialize for `bool`.

| Feature-test macro | Value | Std | Feature |
| --- | --- | --- | --- |
| `__cpp_lib_containers_ranges` | `202202L` | (C++23) | Ranges construction and insertion for containers |

### Example

> Note: This section is incomplete Reason: no example

### Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

| DR | Applied to | Behavior as published | Correct behavior |
| --- | --- | --- | --- |
| LWG 2187 | C++11 | specializations for `bool` lacked `emplace` and `emplace_back` member functions | added |

---
Attribution: content from https://en.cppreference.com/w/cpp/container/vector_bool (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.