vector<bool> 看起来像是一个存放布尔变量的容器,但是其实本身其实并不是一个容器,它里面存放的对象也不是布尔变量,这一点在 GCC 源码中 vector<bool> 的注释中写的很清楚:

/**
* @brief A specialization of vector for booleans which offers fixed time
* access to individual elements in any order.
*
* Note that vector<bool> does not actually meet the requirements for being
* a container. This is because the reference and pointer types are not
* really references and pointers to bool. See DR96 for details. @see
* vector for function documentation.
*
* @ingroup Containers
* @ingroup Sequences
*
* In some terminology a %vector can be described as a dynamic
* C-style array, it offers fast and efficient access to individual
* elements in any order and saves the user from worrying about
* memory and size allocation. Subscripting ( @c [ ] ) access is
* also provided as with C-style arrays.
*/
template<typename _Alloc>
class vector<bool, _Alloc> : protected _Bvector_base<_Alloc>
{
// XXX: real declaration.
}

C++ 标准中对容器的要求中有一条:

如果 c 是支持 [ ] 操作的 T 类型的容器,那么下面的表达式必须可以编译:

\(T* p = \&c[0]\)

但同样是按照 C++ 标准, vector<bool> 作为一个单独的对象,它里面存放的并非真正的 bool, 为了节省内存,其中存放的是 bitfields ,它用其中的每一个 bit 来表示 bool 。 不同于普通的 [ ] 操作符, vector<bool>::[ ] 返回并不是 bool& ,而是一个可以将 vector<bool> 的内部表示转换成 bool 类型的 proxy ,所以表达式:

vector<bool> c;
bool* p = &c[0];

不能编译。

所以,用到 vector<bool> 的时候,切记它并非真正的容器,如果想用真正的存放 bool 类型的容器,需使用 deque

Effective STL 学习笔记 Item 18: 慎用 vector<bool>的更多相关文章

  1. Effective STL 学习笔记 Item 16:vector, string & C API

    有时需要支持 C 的接口,但这并不复杂. 对于 vector 来讲, \(v[0]\) 的地址 \(\&v[0]\) 即可作为数组指针传递给 C API: 1: // Legacy C API ...

  2. Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据

    Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...

  3. Effective STL 学习笔记 Item 30: 保证目标区间足够大

    Effective STL 学习笔记 Item 30: 保证目标区间足够大 */--> div.org-src-container { font-size: 85%; font-family: ...

  4. Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor

    Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor */--> div ...

  5. Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value

    Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value */--> div.org-src-container ...

  6. Effective STL 学习笔记 Item 21:Comparison Function 相关

    Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...

  7. Effective STL 学习笔记 Item 17: Swap Trick

    假设有若干对象存于一个 vector 中: class Widget; vector<Widget> vw; 后来由于某些原因,从该容器中删除了若干对象(参考erase-remove id ...

  8. Effective STL 学习笔记: Item 22 ~ 24

    Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...

  9. Effective STL 学习笔记: 多用 vector & string

    Effective STL 学习笔记: 多用 vector & string 如果可能的话, 尽量避免自己去写动态分配的数组,转而使用 vector 和 string . 原书作者唯一想到的一 ...

随机推荐

  1. Chapter 1(数据结构绪论)

    附件列表 数据结构绪论.jpg

  2. 安装mysql-5.6版本步骤与卸载

    官网下载完解压后: 1.环境变量配置Path   D:\mysql-5.6.40-winx64\bin(你的mySql5.6的路径到bin)2.找到D:\mysql-5.6.40-winx64文件中的 ...

  3. NATS_06:NATS队列验证与监控

    1. NATS 之 Queueing(队列)模式验证 主要以下讲的都是基于 NATS 服务已经开启了(没有开启的请运行:gnatsd 启动):还有请注意所有运行的 go 文件都是在 $GOPATH/s ...

  4. Hadoop生态圈-Hbase过滤器(Filter)

    Hadoop生态圈-Hbase过滤器(Filter) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  5. svn 节点处冲突 解决

    问题描述: [root@localhost ] $ svn up 正在升级 ‘.’: 已跳过 ‘bg0605’ – 节点处于冲突状态 版本 175. 冲突概要: 跳过的路径:1 解决方法: 当前目录下 ...

  6. 在 iPad 上试验从用算法生成法线贴图-到法线映射光照效果

    在 iPad 上试验从用算法生成法线贴图-到法线映射光照效果 目录 概述 一般来说, 法线贴图是用高模的法线图, 低模的纹理图, 来生成较好的渲染效果. 而法线图通常是通过图像处理软件来生成的, 这里 ...

  7. linux ln链接详解

    1.序 Linux具有为一个文件起多个名字的功能,称为链接.被链接的文件可以存放在相同的目录下,但是必须有不同的文件名,而不用在硬盘上为同样的数据重复备份.另外,被链接的文件也可以有相同的文件名,但是 ...

  8. 可视化爬虫Portia安装和部署踩过的坑

    背景 Scrapy爬虫的确是好使好用,去过scrapinghub的官网浏览一下,更是赞叹可视化爬虫的犀利.scrapinghub有一系列的产品,开源了大部分项目,Portia负责可视化爬虫的编辑,Sp ...

  9. 【leetcode 简单】 第六十六题 用栈实现队列

    使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是否为空. ...

  10. git 配置 SSH密钥

    1.登录用户 $ git config --global user.name "geekfeier" $ git config --global user.email " ...