Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/container/vector bool"

From cppreference.com
< cpp‎ | container
(std::vector<bool> isn't necessarily space-optimized)
m (Text replace - "{{concept" to "{{named req")
Line 91: Line 91:
 
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, [http://www.boost.org/doc/libs/release/libs/dynamic_bitset/dynamic_bitset.html boost::dynamic_bitset] exists as an alternative to {{tt|std::vector<bool>}}.
 
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, [http://www.boost.org/doc/libs/release/libs/dynamic_bitset/dynamic_bitset.html boost::dynamic_bitset] exists as an alternative to {{tt|std::vector<bool>}}.
  
Since its representation may by optimized, {{c|std::vector<bool>}} does not necessarily meet all {{concept|Container}} or {{concept|SequenceContainer}} requirements. For example, because {{c|std::vector<bool>::iterator}} is implementation-defined, it may not satisfy the {{concept|ForwardIterator}} requirement.  Use of algorithms such as {{lc|std::search}} that require {{concept|ForwardIterator}}s may result in [http://www.boost.org/doc/libs/1_52_0/libs/dynamic_bitset/dynamic_bitset.html#rationale either compile-time or run-time errors].
+
Since its representation may by 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 [http://www.boost.org/doc/libs/1_52_0/libs/dynamic_bitset/dynamic_bitset.html#rationale either compile-time or run-time errors].
  
  

Revision as of 14:29, 15 June 2018

 
 
 
 
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 (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.

Contents

Member types

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 single bool
(class)
const_reference bool[edit]
pointer implementation-defined[edit]
const_pointer implementation-defined[edit]
iterator

implementation-defined

(until C++20)

implementation-defined ConstexprIterator

(since C++20)
[edit]
const_iterator

implementation-defined

(until C++20)

implementation-defined ConstexprIterator

(since C++20)
[edit]
reverse_iterator std::reverse_iterator<iterator>[edit]
const_reverse_iterator std::reverse_iterator<const_iterator>[edit]

Member functions

constructs the vector
(public member function of std::vector<T,Allocator>) [edit]
destructs the vector
(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]
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
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]
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++14)
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]
constructs elements 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]
vector<bool> specific modifiers
flips all the bits
(public member function) [edit]
[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 vectors
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]

Helper classes

hash support for std::vector<bool>
(class template specialization) [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 LegacyForwardIterator requirement. Use of algorithms such as std::search that require LegacyForwardIterators may result in either compile-time or run-time errors.