(C++17) | ||||
| Sequence | ||||
(C++11) | ||||
vector<bool> | ||||
(C++26) | ||||
(C++26) | ||||
(C++11) | ||||
| Associative | ||||
| Unordered associative | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
| Adaptors | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
| Views | ||||
(C++20) | ||||
(C++23) | ||||
| Tables | ||||
| Iterator invalidation | ||||
| Member function table | ||||
| Non-member function table |
| Member types | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Non-member functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Helper classes | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(C++11) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Deduction guides(C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <vector> | ||
template< class Allocator | ||
std::vector<bool> is a possibly space-efficient specialization ofstd::vector for the typebool.
The manner in whichstd::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 ofsizeof(bool) bytes.
std::vector<bool> behaves similarly tostd::vector, but in order to be space efficient, it:
vector<bool>::reference as a method of accessing individual bits. In particular, objects of this class are returned byoperator[] by value.Contents |
| Member type | Definition | ||||
value_type | bool[edit] | ||||
allocator_type | Allocator[edit] | ||||
size_type | implementation-defined[edit] | ||||
difference_type | implementation-defined[edit] | ||||
| proxy class representing a reference to a singlebool (class) | |||||
const_reference | bool[edit] | ||||
pointer | implementation-defined[edit] | ||||
const_pointer | implementation-defined[edit] | ||||
iterator |
| ||||
const_iterator |
| ||||
reverse_iterator | std::reverse_iterator<iterator>[edit] | ||||
const_reverse_iterator | std::reverse_iterator<const_iterator>[edit] |
constructs thevector(public member function of std::vector<T,Allocator>)[edit] | |
destructs thevector(public member function of std::vector<T,Allocator>)[edit] | |
| assigns values to the container (public member function of std::vector<T,Allocator>)[edit] | |
| assigns values to the container (public member function of std::vector<T,Allocator>)[edit] | |
(C++23) | assigns a range of values to the container (public member function of std::vector<T,Allocator>)[edit] |
| returns the associated allocator (public member function of std::vector<T,Allocator>)[edit] | |
Element access | |
| access specified element with bounds checking (public member function of std::vector<T,Allocator>)[edit] | |
| access specified element (public member function of std::vector<T,Allocator>)[edit] | |
| access the first element (public member function of std::vector<T,Allocator>)[edit] | |
| access the last element (public member function of std::vector<T,Allocator>)[edit] | |
Iterators | |
(C++11) | returns an iterator to the beginning (public member function of std::vector<T,Allocator>)[edit] |
(C++11) | returns an iterator to the end (public member function of std::vector<T,Allocator>)[edit] |
(C++11) | returns a reverse iterator to the beginning (public member function of std::vector<T,Allocator>)[edit] |
(C++11) | returns a reverse iterator to the end (public member function of std::vector<T,Allocator>)[edit] |
Capacity | |
| checks whether the container is empty (public member function of std::vector<T,Allocator>)[edit] | |
| returns the number of elements (public member function of std::vector<T,Allocator>)[edit] | |
| returns the maximum possible number of elements (public member function of std::vector<T,Allocator>)[edit] | |
| reserves storage (public member function of std::vector<T,Allocator>)[edit] | |
| returns the number of elements that can be held in currently allocated storage (public member function of std::vector<T,Allocator>)[edit] | |
Modifiers | |
| clears the contents (public member function of std::vector<T,Allocator>)[edit] | |
| inserts elements (public member function of std::vector<T,Allocator>)[edit] | |
(C++23) | inserts a range of elements (public member function of std::vector<T,Allocator>)[edit] |
(C++23) | adds a range of elements to the end (public member function of std::vector<T,Allocator>)[edit] |
(C++11) | constructs element in-place (public member function of std::vector<T,Allocator>)[edit] |
| erases elements (public member function of std::vector<T,Allocator>)[edit] | |
| adds an element to the end (public member function of std::vector<T,Allocator>)[edit] | |
(C++11) | constructs an element in-place at the end (public member function of std::vector<T,Allocator>)[edit] |
| removes the last element (public member function of std::vector<T,Allocator>)[edit] | |
| changes the number of elements stored (public member function of std::vector<T,Allocator>)[edit] | |
| swaps the contents (public member function of std::vector<T,Allocator>)[edit] | |
| |
| flips all the bits (public member function)[edit] | |
[static] | swaps twostd::vector<bool>::references(public static member function)[edit] |
(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(C++20) | lexicographically compares the values of twovectors(function template)[edit] |
| specializes thestd::swap algorithm (function template)[edit] | |
| erases all elements satisfying specific criteria (function template)[edit] |
(C++11) | hash support forstd::vector<bool> (class template specialization)[edit] |
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 tostd::vector<bool>.
Since its representation may be optimized,std::vector<bool> does not necessarily meet allContainer orSequenceContainer requirements. For example, becausestd::vector<bool>::iterator is implementation-defined, it may not satisfy theLegacyForwardIterator requirement. Use of algorithms such asstd::search that requireLegacyForwardIterators may result ineither compile-time or run-time errors.
TheBoost.Container version ofvector does not specialize forbool.
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_containers_ranges | 202202L | (C++23) | Ranges construction and insertion for containers |
#include <cassert>#include <initializer_list>#include <iostream>#include <vector> void println(auto rem,conststd::vector<bool>& vb){std::cout<< rem<<" = [";for(std::size_t t{}; t!= vb.size();++t)std::cout<<(t?", ":"")<< vb[t];std::cout<<"]\n";} int main(){std::vector<bool> v1;// creates an empty vector of boolean values println("1) v1", v1); std::vector<bool> v2{0,1,1,0,1};// creates filled vector println("2) v2", v2); v1= v2;// copies v2 to v1 println("3) v1", v1); assert(v1.size()== v2.size());// checks that v1 and v2 sizes are equalassert(v1.front()==false);// accesses first element, equivalent to:assert(v1[0]==false);assert(v1.back()==true);// accesses last element, equivalent to:assert(v1[v1.size()-1]==true); v1={true,true,false,false};// assigns an initializer list println("4) v1", v1); v1.push_back(true);// adds one element to the end println("5) v1", v1); v1.pop_back();// removes one element from the end println("6) v1", v1); v1.flip();// flips all elements println("7) v1", v1); v1.resize(8,true);// resizes v1; new elements are set to “true” println("8) v1", v1); v1.clear();// erases v1assert(v1.empty());// checks that v1 is empty}
Output:
1) v1 = []2) v2 = [0, 1, 1, 0, 1]3) v1 = [0, 1, 1, 0, 1]4) v1 = [1, 1, 0, 0]5) v1 = [1, 1, 0, 0, 1]6) v1 = [1, 1, 0, 0]7) v1 = [0, 0, 1, 1]8) v1 = [0, 0, 1, 1, 1, 1, 1, 1]
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 forbool lackedemplace andemplace_back member functions | added |