std::vector<bool>
来自 cppreference.cn
在头文件 <vector> 中定义 |
||
template< class Allocator |
||
std::vector
<bool> 是 bool 类型 std::vector 的可能节省空间的特化。
std::vector
<bool> 节省空间的方式(以及是否进行了优化)是实现定义的。一种潜在的优化涉及合并向量元素,使每个元素占用一个位而不是 sizeof(bool) 字节。
std::vector
<bool> 的行为与 std::vector 类似,但为了节省空间,它:
- 不一定将元素存储为连续数组。
- 暴露类 std::
vector
<bool>::reference
作为访问单个位的方法。特别是,operator[] 返回此类的对象。 - 不使用 std::allocator_traits::construct 构造位值。
- 不保证同一容器中的不同元素可以由不同线程并发修改。
目录 |
[编辑] 成员类型
成员类型 | 定义 | ||||
value_type
|
bool | ||||
allocator_type
|
Allocator | ||||
size_type
|
实现定义 | ||||
difference_type
|
实现定义 | ||||
表示对单个 bool 的引用的代理类 (类) | |||||
const_reference
|
bool | ||||
pointer
|
实现定义 | ||||
const_pointer
|
实现定义 | ||||
iterator
|
| ||||
const_iterator
|
| ||||
reverse_iterator
|
std::reverse_iterator<iterator> | ||||
const_reverse_iterator
|
std::reverse_iterator<const_iterator> |
[编辑] 成员函数
构造 vector ( std::vector<T,Allocator> 的公共成员函数) | |
析构 vector ( std::vector<T,Allocator> 的公共成员函数) | |
将值赋给容器 ( std::vector<T,Allocator> 的公共成员函数) | |
将值赋给容器 ( std::vector<T,Allocator> 的公共成员函数) | |
(C++23) |
将一个范围的值赋给容器 ( std::vector<T,Allocator> 的公共成员函数) |
返回关联的分配器 ( std::vector<T,Allocator> 的公共成员函数) | |
元素访问 | |
访问指定的元素,带边界检查 ( std::vector<T,Allocator> 的公共成员函数) | |
访问指定的元素 ( std::vector<T,Allocator> 的公共成员函数) | |
访问第一个元素 ( std::vector<T,Allocator> 的公共成员函数) | |
访问最后一个元素 ( std::vector<T,Allocator> 的公共成员函数) | |
迭代器 | |
(C++11) |
返回指向起始的迭代器 ( std::vector<T,Allocator> 的公共成员函数) |
(C++11) |
返回指向末尾的迭代器 ( std::vector<T,Allocator> 的公共成员函数) |
(C++11) |
返回指向起始的逆向迭代器 ( std::vector<T,Allocator> 的公共成员函数) |
(C++11) |
返回指向末尾的逆向迭代器 ( std::vector<T,Allocator> 的公共成员函数) |
容量 | |
检查容器是否为空 ( std::vector<T,Allocator> 的公共成员函数) | |
返回元素数量 ( std::vector<T,Allocator> 的公共成员函数) | |
返回元素的最大可能数量 ( std::vector<T,Allocator> 的公共成员函数) | |
预留存储空间 ( std::vector<T,Allocator> 的公共成员函数) | |
返回当前已分配存储空间中可容纳的元素数量 ( std::vector<T,Allocator> 的公共成员函数) | |
修改器 | |
清除内容 ( std::vector<T,Allocator> 的公共成员函数) | |
插入元素 ( std::vector<T,Allocator> 的公共成员函数) | |
(C++23) |
插入元素范围 ( std::vector<T,Allocator> 的公共成员函数) |
(C++23) |
添加一个元素范围到结尾 ( std::vector<T,Allocator> 的公共成员函数) |
(C++11) |
就地构造元素 ( std::vector<T,Allocator> 的公共成员函数) |
擦除元素 ( std::vector<T,Allocator> 的公共成员函数) | |
添加元素到结尾 ( std::vector<T,Allocator> 的公共成员函数) | |
(C++11) |
就地构造元素于结尾 ( std::vector<T,Allocator> 的公共成员函数) |
移除末元素 ( std::vector<T,Allocator> 的公共成员函数) | |
更改存储的元素数量 ( std::vector<T,Allocator> 的公共成员函数) | |
交换内容 ( std::vector<T,Allocator> 的公共成员函数) | |
| |
翻转所有位 (公共成员函数) | |
[静态] |
交换两个 std::vector<bool>:: reference(公共静态成员函数) |
[编辑] 非成员函数
(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(C++20) |
字典序比较两个 vector 的值(函数模板) |
特化 std::swap 算法 (函数模板) | |
擦除所有满足特定标准的元素 (函数模板) |
[编辑] 辅助类
(C++11) |
对 std::vector<bool> 的哈希支持 (类模板特化) |
[编辑] 推导指南 (C++17)
[编辑] 注意
如果位集的尺寸在编译时已知,可以使用 std::bitset,它提供了更丰富的成员函数集。此外,还存在 boost::dynamic_bitset
作为 std::vector
<bool> 的替代方案。
由于其表示可能经过优化,std::vector
<bool> 不一定满足所有 Container 或 SequenceContainer 的要求。例如,由于 std::vector
<bool>::iterator 是实现定义的,它可能不满足 LegacyForwardIterator 的要求。使用需要 LegacyForwardIterator 的算法,例如 std::search,可能会导致 编译时或运行时错误。
Boost.Container 版本的 vector
不对 bool 进行特化。
特性测试宏 | 值 | 标准 | 特性 |
---|---|---|---|
__cpp_lib_containers_ranges |
202202L |
(C++23) | 容器的范围构造和插入 |
[编辑] 示例
本节不完整 原因:无示例 |
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 2187 | C++11 | 对 bool 的特化缺少 emplace 和 emplace_back 成员函数 |
已添加 |