Difference between revisions of "cpp/container/vector bool"
(add links, remove '&v[0] + n != &v[n]' since it's ill-formed) |
m (→Notes: +FTM) |
||
(7 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{cpp/title|vector<bool>}} | {{cpp/title|vector<bool>}} | ||
{{cpp/container/vector_bool/navbar}} | {{cpp/container/vector_bool/navbar}} | ||
− | {{ddcl | header=vector | | + | {{ddcl|header=vector| |
− | template<class Allocator | + | template< |
− | class vector<bool, Allocator>; | + | class Allocator |
− | }} | + | > class vector<bool, Allocator>; |
+ | }} | ||
− | {{c|std::vector<bool>}} is a possibly space-efficient specialization of {{lc|std::vector}} for the type {{c|bool}}. | + | {{c|std::vector<bool>}} is a possibly space-efficient specialization of {{lc|std::vector}} for the type {{c|bool}}. |
The manner in which {{c|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 {{c|sizeof(bool)}} bytes. | The manner in which {{c|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 {{c|sizeof(bool)}} bytes. | ||
Line 19: | Line 20: | ||
===Member types=== | ===Member types=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc hitem | Member type | Definition}} | + | {{dsc hitem|Member type|Definition}} |
− | {{dsc inc | cpp/container/dsc value_type | vector_bool}} | + | {{dsc inc|cpp/container/dsc value_type|vector_bool}} |
− | {{dsc inc | cpp/container/dsc allocator_type | vector_bool}} | + | {{dsc inc|cpp/container/dsc allocator_type|vector_bool}} |
− | {{dsc inc | cpp/container/dsc size_type | vector_bool}} | + | {{dsc inc|cpp/container/dsc size_type|vector_bool}} |
− | {{dsc inc | cpp/container/dsc difference_type | vector_bool}} | + | {{dsc inc|cpp/container/dsc difference_type|vector_bool}} |
− | {{dsc class | cpp/container/vector_bool/reference | proxy class representing a reference to a single bool}} | + | {{dsc class|cpp/container/vector_bool/reference|proxy class representing a reference to a single bool}} |
− | {{dsc inc | cpp/container/dsc const_reference | vector_bool}} | + | {{dsc inc|cpp/container/dsc const_reference|vector_bool}} |
− | {{dsc inc | cpp/container/dsc pointer | vector_bool}} | + | {{dsc inc|cpp/container/dsc pointer|vector_bool}} |
− | {{dsc inc | cpp/container/dsc const_pointer | vector_bool}} | + | {{dsc inc|cpp/container/dsc const_pointer|vector_bool}} |
− | {{dsc inc | cpp/container/dsc iterator | vector_bool}} | + | {{dsc inc|cpp/container/dsc iterator|vector_bool}} |
− | {{dsc inc | cpp/container/dsc const_iterator | vector_bool}} | + | {{dsc inc|cpp/container/dsc const_iterator|vector_bool}} |
− | {{dsc inc | cpp/container/dsc reverse_iterator | vector_bool}} | + | {{dsc inc|cpp/container/dsc reverse_iterator|vector_bool}} |
− | {{dsc inc | cpp/container/dsc const_reverse_iterator | vector_bool}} | + | {{dsc inc|cpp/container/dsc const_reverse_iterator|vector_bool}} |
{{dsc end}} | {{dsc end}} | ||
===Member functions=== | ===Member functions=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/container/dsc constructor | vector}} | + | {{dsc inc|cpp/container/dsc constructor|vector}} |
− | {{dsc inc | cpp/container/dsc destructor | vector}} | + | {{dsc inc|cpp/container/dsc destructor|vector}} |
− | {{dsc inc | cpp/container/dsc operator{{=}} | vector}} | + | {{dsc inc|cpp/container/dsc operator{{=}}|vector}} |
− | {{dsc inc | cpp/container/dsc assign | vector}} | + | {{dsc inc|cpp/container/dsc assign|vector}} |
− | {{dsc inc | cpp/container/dsc get_allocator | vector}} | + | {{dsc inc|cpp/container/dsc get_allocator|vector}} |
− | {{dsc h2 | Element access}} | + | {{dsc h2|Element access}} |
− | {{dsc inc | cpp/container/dsc at | vector}} | + | {{dsc inc|cpp/container/dsc at|vector}} |
− | {{dsc inc | cpp/container/dsc operator_at | vector}} | + | {{dsc inc|cpp/container/dsc operator_at|vector}} |
− | {{dsc inc | cpp/container/dsc front | vector}} | + | {{dsc inc|cpp/container/dsc front|vector}} |
− | {{dsc inc | cpp/container/dsc back | vector}} | + | {{dsc inc|cpp/container/dsc back|vector}} |
− | {{dsc h2 | Iterators}} | + | {{dsc h2|Iterators}} |
− | {{dsc inc | cpp/container/dsc begin | vector}} | + | {{dsc inc|cpp/container/dsc begin|vector}} |
− | {{dsc inc | cpp/container/dsc end | vector}} | + | {{dsc inc|cpp/container/dsc end|vector}} |
− | {{dsc inc | cpp/container/dsc rbegin | vector}} | + | {{dsc inc|cpp/container/dsc rbegin|vector}} |
− | {{dsc inc | cpp/container/dsc rend | vector}} | + | {{dsc inc|cpp/container/dsc rend|vector}} |
− | {{dsc h2 | Capacity}} | + | {{dsc h2|Capacity}} |
− | {{dsc inc | cpp/container/dsc empty | vector}} | + | {{dsc inc|cpp/container/dsc empty|vector}} |
− | {{dsc inc | cpp/container/dsc size | vector}} | + | {{dsc inc|cpp/container/dsc size|vector}} |
− | {{dsc inc | cpp/container/dsc max_size | vector}} | + | {{dsc inc|cpp/container/dsc max_size|vector}} |
− | {{dsc inc | cpp/container/dsc reserve | vector}} | + | {{dsc inc|cpp/container/dsc reserve|vector}} |
− | {{dsc inc | cpp/container/dsc capacity | vector}} | + | {{dsc inc|cpp/container/dsc capacity|vector}} |
− | {{dsc h2 | Modifiers}} | + | {{dsc h2|Modifiers}} |
− | {{dsc inc | cpp/container/dsc clear | vector}} | + | {{dsc inc|cpp/container/dsc clear|vector}} |
− | {{dsc inc | cpp/container/dsc insert | vector}} | + | {{dsc inc|cpp/container/dsc insert|vector}} |
− | {{dsc inc | cpp/container | + | {{dsc inc|cpp/container/dsc emplace|vector}} |
− | {{dsc inc | cpp/container/dsc erase | vector}} | + | {{dsc inc|cpp/container/dsc erase|vector}} |
− | {{dsc inc | cpp/container/dsc push_back | vector}} | + | {{dsc inc|cpp/container/dsc push_back|vector}} |
− | {{dsc inc | cpp/container | + | {{dsc inc|cpp/container/dsc emplace_back|vector}} |
− | {{dsc inc | cpp/container/dsc pop_back | vector}} | + | {{dsc inc|cpp/container/dsc pop_back|vector}} |
− | {{dsc inc | cpp/container/dsc resize | vector}} | + | {{dsc inc|cpp/container/dsc resize|vector}} |
− | {{dsc inc | cpp/container/dsc swap | vector}} | + | {{dsc inc|cpp/container/dsc swap|vector}} |
− | {{dsc h2 | {{tt|vector<bool>}} specific modifiers}} | + | {{dsc h2|{{tt|vector<bool>}} specific modifiers}} |
− | {{dsc inc | cpp/container/vector_bool/dsc flip}} | + | {{dsc inc|cpp/container/vector_bool/dsc flip}} |
− | {{dsc inc | cpp/container/vector_bool/dsc swap}} | + | {{dsc inc|cpp/container/vector_bool/dsc swap}} |
{{dsc end}} | {{dsc end}} | ||
===Non-member functions=== | ===Non-member functions=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/container/dsc operator_cmp | vector}} | + | {{dsc inc|cpp/container/dsc operator_cmp|vector}} |
− | {{dsc inc | cpp/container/dsc swap2 | vector}} | + | {{dsc inc|cpp/container/dsc swap2|vector}} |
− | {{dsc inc | cpp/container/dsc erase seq | vector}} | + | {{dsc inc|cpp/container/dsc erase seq|vector}} |
{{dsc end}} | {{dsc end}} | ||
===Helper classes=== | ===Helper classes=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/container/vector_bool/dsc hash}} | + | {{dsc inc|cpp/container/vector_bool/dsc hash}} |
{{dsc end}} | {{dsc end}} | ||
− | ===[[cpp/container/vector/deduction guides|Deduction guides]]{{mark c++17}}=== | + | ===[[cpp/container/vector/deduction guides|Deduction guides]] {{mark c++17}}=== |
===Notes=== | ===Notes=== | ||
− | If the size of the bitset is known at compile time, {{lc|std::bitset}} may be used, which offers a richer set of member functions. In addition, [ | + | If the size of the bitset is known at compile time, {{lc|std::bitset}} may be used, which offers a richer set of member functions. In addition, [https://www.boost.org/doc/libs/release/libs/dynamic_bitset/dynamic_bitset.html {{tt|boost::dynamic_bitset}}] exists as an alternative to {{tt|std::vector<bool>}}. |
− | Since its representation may be optimized, {{c|std::vector<bool>}} does not necessarily meet all {{named req|Container}} or {{named req|SequenceContainer}} requirements. For example, because {{c|std::vector<bool>::iterator}} is implementation-defined, it may not satisfy the {{named req|ForwardIterator}} requirement. Use of algorithms such as {{lc|std::search}} that require {{named req|ForwardIterator}}s may result in [ | + | Since its representation may be optimized, {{c|std::vector<bool>}} does not necessarily meet all {{named req|Container}} or {{named req|SequenceContainer}} requirements. For example, because {{c|std::vector<bool>::iterator}} is implementation-defined, it may not satisfy the {{named req|ForwardIterator}} requirement. Use of algorithms such as {{lc|std::search}} that require {{named req|ForwardIterator}}s may result in [https://www.boost.org/doc/libs/release/libs/dynamic_bitset/dynamic_bitset.html#rationale either compile-time or run-time errors]. |
− | The [https://www.boost.org/doc/libs/ | + | The [https://www.boost.org/doc/libs/release/doc/html/boost/container/vector.html Boost.Container version of {{tt|vector}}] does not specialize for {{c|bool}}. |
+ | |||
+ | ===Notes=== | ||
+ | {{ftm begin|std=1|comment=1}} | ||
+ | {{ftm|__cpp_lib_containers_ranges|value=202202L|std=C++23|Ranges construction and insertion for containers}} | ||
+ | {{ftm end}} | ||
+ | |||
+ | ===Defect reports=== | ||
+ | {{dr list begin}} | ||
+ | {{dr list item|wg=lwg|dr=2187|std=C++11|before=specializations for {{c|bool}} lacked {{tt|emplace}} and {{tt|emplace_back}} member functions|after=added}} | ||
+ | {{dr list end}} | ||
{{langlinks|de|es|fr|it|ja|pt|ru|zh}} | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 10:50, 29 April 2023
Defined in header <vector>
|
||
template< class 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.
Contents |
[edit] Member types
Member type | Definition | ||||
value_type
|
bool | ||||
allocator_type
|
Allocator
| ||||
size_type
|
implementation-defined | ||||
difference_type
|
implementation-defined | ||||
proxy class representing a reference to a single bool (class) | |||||
const_reference
|
bool | ||||
pointer
|
implementation-defined | ||||
const_pointer
|
implementation-defined | ||||
iterator
|
| ||||
const_iterator
|
| ||||
reverse_iterator
|
std::reverse_iterator<iterator> | ||||
const_reverse_iterator
|
std::reverse_iterator<const_iterator> |
[edit] Member functions
constructs the vector (public member function of std::vector<T,Allocator> )
| |
destructs the vector (public member function of std::vector<T,Allocator> )
| |
assigns values to the container (public member function of std::vector<T,Allocator> )
| |
assigns values to the container (public member function of std::vector<T,Allocator> )
| |
returns the associated allocator (public member function of std::vector<T,Allocator> )
| |
Element access | |
access specified element with bounds checking (public member function of std::vector<T,Allocator> )
| |
access specified element (public member function of std::vector<T,Allocator> )
| |
access the first element (public member function of std::vector<T,Allocator> )
| |
access the last element (public member function of std::vector<T,Allocator> )
| |
Iterators | |
(C++11) |
returns an iterator to the beginning (public member function of std::vector<T,Allocator> )
|
(C++11) |
returns an iterator to the end (public member function of std::vector<T,Allocator> )
|
(C++11) |
returns a reverse iterator to the beginning (public member function of std::vector<T,Allocator> )
|
(C++11) |
returns a reverse iterator to the end (public member function of std::vector<T,Allocator> )
|
Capacity | |
checks whether the container is empty (public member function of std::vector<T,Allocator> )
| |
returns the number of elements (public member function of std::vector<T,Allocator> )
| |
returns the maximum possible number of elements (public member function of std::vector<T,Allocator> )
| |
reserves storage (public member function of std::vector<T,Allocator> )
| |
returns the number of elements that can be held in currently allocated storage (public member function of std::vector<T,Allocator> )
| |
Modifiers | |
clears the contents (public member function of std::vector<T,Allocator> )
| |
inserts elements (public member function of std::vector<T,Allocator> )
| |
(C++11) |
constructs element in-place (public member function of std::vector<T,Allocator> )
|
erases elements (public member function of std::vector<T,Allocator> )
| |
adds an element to the end (public member function of std::vector<T,Allocator> )
| |
(C++11) |
constructs an element in-place at the end (public member function of std::vector<T,Allocator> )
|
removes the last element (public member function of std::vector<T,Allocator> )
| |
changes the number of elements stored (public member function of std::vector<T,Allocator> )
| |
swaps the contents (public member function of std::vector<T,Allocator> )
| |
| |
flips all the bits (public member function) | |
[static] |
swaps two std::vector<bool>:: references (public static member function) |
[edit] Non-member functions
(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 two vector s (function template) |
specializes the std::swap algorithm (function template) | |
erases all elements satisfying specific criteria (function template) |
[edit] Helper classes
(C++11) |
hash support for std::vector<bool> (class template specialization) |
[edit] Deduction guides (C++17)
[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 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 LegacyForwardIterators may result in either compile-time or run-time errors.
The Boost.Container version of vector
does not specialize for bool.
[edit] Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_containers_ranges |
202202L | (C++23) | Ranges construction and insertion for containers |
[edit] 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 |