std::vector<bool>
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    |   定义于头文件  <vector>
  | 
||
|   template<class Allocator>  class vector<bool, 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
 | 
实现定义 | 
  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> 的公开成员函数)  | |
|    返回相关的分配器   ( 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++14 起)  | 
   原位构造元素   ( std::vector<T,Allocator> 的公开成员函数)  | 
|   擦除元素  ( std::vector<T,Allocator> 的公开成员函数)  | |
|    将元素添加到容器末尾  ( std::vector<T,Allocator> 的公开成员函数)  | |
|    (C++14)  | 
   在末尾原位构造元素  ( 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 可能导致编译时或运行时错误。
vector 的 Boost.Container 版本不对 bool 特化。