##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::vector<bool>

From cppreference.com
< cpp‎ | container

Defined in header `<vector>` |  |
template<class Allocator> class vector<bool, Allocator>; |  |

std::vector<bool> is a 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 (so &v[0] + n != &v[n])
- 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.

### [edit] Member types

Member type | Definition
`value_type` | bool [edit]
`allocator_type` | `Allocator` [edit]
`size_type` | implementation-defined [edit]
`difference_type` | implementation-defined[edit]
reference | proxy class representing a reference to a single bool (class)
`const_reference` | bool [edit]
`pointer` | implementation-defined[edit]
`const_pointer` | implementation-defined[edit]
`iterator` | implementation-defined [edit]
`const_iterator` | implementation-defined [edit]
`reverse_iterator` | std::reverse_iterator<iterator> [edit]
`const_reverse_iterator` | std::reverse_iterator<const_iterator> [edit]

### [edit] Member functions

(constructor) | constructs the `vector` (public member function of `std::vector`) [edit]
(destructor) | destructs the `vector` (public member function of `std::vector`) [edit]
operator= | assigns values to the container (public member function of `std::vector`) [edit]
assign | assigns values to the container (public member function of `std::vector`) [edit]
get_allocator | returns the associated allocator (public member function of `std::vector`) [edit]
##### Element access
at | access specified element with bounds checking (public member function of `std::vector`) [edit]
operator[] | access specified element (public member function of `std::vector`) [edit]
front | access the first element (public member function of `std::vector`) [edit]
back | access the last element (public member function of `std::vector`) [edit]
##### Iterators
begin cbegin | returns an iterator to the beginning (public member function of `std::vector`) [edit]
end cend | returns an iterator to the end (public member function of `std::vector`) [edit]
rbegin crbegin | returns a reverse iterator to the beginning (public member function of `std::vector`) [edit]
rend crend | returns a reverse iterator to the end (public member function of `std::vector`) [edit]
##### Capacity
empty | checks whether the container is empty (public member function of `std::vector`) [edit]
size | returns the number of elements (public member function of `std::vector`) [edit]
max_size | returns the maximum possible number of elements (public member function of `std::vector`) [edit]
reserve | reserves storage (public member function of `std::vector`) [edit]
capacity | returns the number of elements that can be held in currently allocated storage (public member function of `std::vector`) [edit]
##### Modifiers
clear | clears the contents (public member function of `std::vector`) [edit]
insert | inserts elements (public member function of `std::vector`) [edit]
emplace (since C++14) | constructs element in-place (public member function of `std::vector`) [edit]
erase | erases elements (public member function of `std::vector`) [edit]
push_back | adds an element to the end (public member function of `std::vector`) [edit]
emplace_back (C++14) | constructs elements in-place at the end (public member function of `std::vector`) [edit]
pop_back | removes the last element (public member function of `std::vector`) [edit]
resize | changes the number of elements stored (public member function of `std::vector`) [edit]
swap | swaps the contents (public member function of `std::vector`) [edit]
##### `vector<bool>` specific modifiers
flip | flips all the bits (public member function) [edit]
swap [static] | swaps two `std::vector<bool>::reference`s (public static member function) [edit]

### [edit] Non-member functions

operator==operator!=operator<operator<=operator>operator>= | lexicographically compares the values in the vector (function template) [edit]
std::swap(std::vector) | specializes the std::swap algorithm (function template) [edit]

### [edit] Helper classes

std::hash<std::vector<bool>> (C++11) | hash support for std::vector<bool> (class template specialization) [edit]

### [edit] 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 by 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 `ForwardIterator` requirement. Use of algorithms such as std::search that require `ForwardIterator`s may result in either compile-time or run-time errors.

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/container/vector_bool&oldid=85877"