C++11 standard template (STL) std::vector<bool> (1)

Defined in the header file <vector>

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

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

The space-efficient behavior of std::vector<bool> (and whether it has optimizations) is implementation-defined. One potential optimization involves unioning the elements of the vector so that each element occupies a single bit, rather than sizeof(bool) bytes.

std::vector<bool> behaves like std::vector, but to save space it:

  • elements don't have to be stored as contiguous arrays (so &v[0] + n != &v[n] )
  • Expose class std::vector<bool>::reference as methods for accessing individual bits. In particular, the class of this type is returned by value by operator[].
  • Bit values ​​are not constructed using std::allocator_traits::construct.
  • There is no guarantee that different elements in the same container can be modified concurrently by different threads.

member type

member type definition
value_type bool
allocator_type Allocator
size_type implementation-defined
difference_type implementation-defined

reference


A proxy class (class) representing a reference to a single bool
const_reference bool
pointer implementation-defined
const_pointer implementation-defined
iterator implementation-defined
const_iterator implementation-defined
reverse_iterator std::reverse_iterator<iterator>
const_reverse_iterator std::reverse_iterator<const_iterator>

member function

(Constructor)

constructor vector
( std::vector<T,Allocator>public member function of)

(destructor)

Destructor vector
( std::vector<T,Allocator>public member function of)

operator=

Assign to container
( std::vector<T,Allocator>public member function of)

assign

Assign value to container
( std::vector<T,Allocator>public member function of)

get_allocator

Returns the associated allocator
( std::vector<T,Allocator>public member function of

element access

at

Access the specified element while performing out-of-bounds checks
( std::vector<T,Allocator>public member functions)

operator[]

Access the specified element
( std::vector<T,Allocator>public member function of)

front

Access the first element
( std::vector<T,Allocator>public member function of

back

Access the last element
( std::vector<T,Allocator>public member function of

iterator

begin cbegin

Returns an iterator pointing to the first element of the container
( std::vector<T,Allocator>public member function of

end cend

Returns an iterator pointing to the end of the container
( std::vector<T,Allocator>public member function of

rbegin crbegin

Returns a reverse iterator pointing to the last element of the container
( std::vector<T,Allocator>public member function of

rend crend

Returns a reverse iterator pointing to the front
( std::vector<T,Allocator>public member function of

container

empty

Check if container is empty
( std::vector<T,Allocator>public member function of

size

Returns the number of elements held
( std::vector<T,Allocator>public member function of

max_size

返回可容纳的最大元素数
(std::vector<T,Allocator> 的公开成员函数)

reserve

预留存储空间
(std::vector<T,Allocator> 的公开成员函数)

capacity

返回当前存储空间能够容纳的元素数
(std::vector<T,Allocator> 的公开成员函数)

修改器

clear

清除内容
(std::vector<T,Allocator> 的公开成员函数)

insert

插入元素
(std::vector<T,Allocator> 的公开成员函数)

emplace

(C++14 起)

原位构造元素
(std::vector<T,Allocator> 的公开成员函数)

erase

擦除元素
(std::vector<T,Allocator> 的公开成员函数)

push_back

将元素添加到容器末尾
(std::vector<T,Allocator> 的公开成员函数)

emplace_back

(C++14)

在末尾原位构造元素
(std::vector<T,Allocator> 的公开成员函数)

pop_back

移除末元素
(std::vector<T,Allocator> 的公开成员函数)

resize

改变容器中可存储元素的个数
(std::vector<T,Allocator> 的公开成员函数)

swap

交换内容
(std::vector<T,Allocator> 的公开成员函数)

vector<bool> 特定修改器

flip

翻转所有位
(公开成员函数)

swap

[静态]

交换二个 std::vector<bool>::reference
(公开静态成员函数)

非成员函数

operator==operator!=operator<operator<=operator>operator>=

按照字典顺序比较 vector 中的值
(函数模板)

std::swap(std::vector)

特化 std::swap 算法
(函数模板)

帮助类

std::hash<std::vector<bool>>

(C++11)

std::vector<bool> 的哈希支持
(类模板特化)

注意

若位集的大小在编译时已知,可使用 std::bitset ,它提供一组更丰富的成员函数。另外, boost::dynamic_bitset 作为 std::vector<bool> 的替用者存在。

因为 std::vector<bool> 的表示可以优化,故它不需要满足所有容器 (Container) 或序列容器 (SequenceContainer) 要求。例如,因为 std::vector<bool>::iterator 是实现定义的,故它可以不满足 遗留向前迭代器 (LegacyForwardIterator) 要求。使用要求 遗留向前迭代器 (LegacyForwardIterator) 的算法,例如 std::search 可能导致编译时或运行时错误

Guess you like

Origin blog.csdn.net/qq_40788199/article/details/130544960