std::vector<bool>
来自 cppreference.cn
定义于头文件 <vector> |
||
template< class Allocator |
||
std::vector
<bool> 是 std::vector 对于类型 bool 的一个可能节省空间的特化。
std::vector
<bool> 如何实现空间效率(以及是否进行了优化)是实现定义的。一种可能的优化包括合并 vector 元素,使得每个元素占用一个比特而不是 sizeof(bool) 字节。
std::vector
<bool> 的行为类似于 std::vector,但为了实现空间效率,它
- 不一定将其元素存储为连续数组。
- 暴露类 std::
vector
<bool>::reference
作为访问单个比特的方法。 特别是,此类的对象由 operator[] 按值返回。 - 不使用 std::allocator_traits::construct 来构造比特值。
- 不保证同一容器中的不同元素可以被不同线程并发修改。
目录 |
[编辑] 成员类型
成员类型 | 定义 | ||||
value_type
|
bool | ||||
allocator_type
|
Allocator | ||||
size_type
|
implementation-defined | ||||
difference_type
|
implementation-defined | ||||
代理类,表示对单个 bool 的引用 (类) | |||||
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> |
[编辑] 成员函数
构造 vector (public member function of std::vector<T,Allocator> ) | |
析构 vector (public member function of std::vector<T,Allocator> ) | |
为容器赋值 (public member function of std::vector<T,Allocator> ) | |
为容器赋值 (public member function of std::vector<T,Allocator> ) | |
(C++23) |
为容器赋值一个值范围 (public member function of std::vector<T,Allocator> ) |
返回关联的分配器 (public member function of std::vector<T,Allocator> ) | |
元素访问 | |
访问指定元素,带边界检查 (public member function of std::vector<T,Allocator> ) | |
访问指定元素 (public member function of std::vector<T,Allocator> ) | |
访问首元素 (public member function of std::vector<T,Allocator> ) | |
访问末元素 (public member function of std::vector<T,Allocator> ) | |
迭代器 | |
(C++11) |
返回指向开头的迭代器 (public member function of std::vector<T,Allocator> ) |
(C++11) |
返回指向末尾的迭代器 (public member function of std::vector<T,Allocator> ) |
(C++11) |
返回指向开头的逆向迭代器 (public member function of std::vector<T,Allocator> ) |
(C++11) |
返回指向末尾的逆向迭代器 (public member function of std::vector<T,Allocator> ) |
容量 | |
检查容器是否为空 (public member function of std::vector<T,Allocator> ) | |
返回元素数量 (public member function of std::vector<T,Allocator> ) | |
返回最大可能元素数量 (public member function of std::vector<T,Allocator> ) | |
预留存储空间 (public member function of std::vector<T,Allocator> ) | |
返回当前已分配存储空间中可容纳的元素数 (public member function of std::vector<T,Allocator> ) | |
修改器 | |
清空内容 (public member function of std::vector<T,Allocator> ) | |
插入元素 (public member function of std::vector<T,Allocator> ) | |
(C++23) |
插入一个元素范围 (public member function of std::vector<T,Allocator> ) |
(C++23) |
在末尾添加一个元素范围 (public member function of std::vector<T,Allocator> ) |
(C++11) |
就地构造元素 (public member function of std::vector<T,Allocator> ) |
移除元素 (public member function of std::vector<T,Allocator> ) | |
在末尾添加一个元素 (public member function of std::vector<T,Allocator> ) | |
(C++11) |
在末尾就地构造一个元素 (public member function of std::vector<T,Allocator> ) |
移除末尾元素 (public member function of std::vector<T,Allocator> ) | |
更改存储的元素数量 (public member function of std::vector<T,Allocator> ) | |
交换内容 (public member function of std::vector<T,Allocator> ) | |
| |
翻转所有比特位 (public member function) | |
[static] |
交换两个 std::vector<bool>:: references(public static member function) |
[编辑] 非成员函数
(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(C++20) |
字典序比较两个 vector 的值(function template) |
特化 std::swap 算法 (function template) | |
移除所有满足特定标准的元素 (function template) |
[编辑] 辅助类
(C++11) |
为 std::vector<bool> 提供哈希支持 (class template specialization) |
[编辑] Deduction guides (C++17)
[编辑] 注解
如果 bitset 的大小在编译时已知,则可以使用 std::bitset,它提供更丰富的成员函数集。 此外,boost::dynamic_bitset
作为 std::vector
<bool> 的替代品而存在。
由于其表示形式可能被优化,std::vector
<bool> 不一定满足所有 Container 或 SequenceContainer 要求。 例如,由于 std::vector
<bool>::iterator 是实现定义的,因此它可能不满足 LegacyForwardIterator 的要求。 使用诸如 std::search 等需要 LegacyForwardIterators 的算法可能会导致 编译时或运行时错误。
Boost.Container 版本的 vector
不会为 bool 特化。
Feature-test 宏 | 值 | Std | 功能 |
---|---|---|---|
__cpp_lib_containers_ranges |
202202L |
(C++23) | 容器的范围构造和插入 |
[编辑] 示例
本节尚不完整 原因:没有示例 |
[编辑] 缺陷报告
以下行为变更缺陷报告已追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 2187 | C++11 | bool 的特化缺少 emplace 和 emplace_back 成员函数 |
已添加 |