Effective STL 学习笔记 Item 18: 慎用 vector<bool>
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 。
(使用许可:署名-非商业性使用-相同方式共享 3.0 中国大陆许可协议 。)
Effective STL 学习笔记 Item 18: 慎用 vector<bool>的更多相关文章
- Effective STL 学习笔记 Item 16:vector, string & C API
		
有时需要支持 C 的接口,但这并不复杂. 对于 vector 来讲, \(v[0]\) 的地址 \(\&v[0]\) 即可作为数组指针传递给 C API: 1: // Legacy C API ...
 - Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据
		
Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...
 - Effective STL 学习笔记 Item 30: 保证目标区间足够大
		
Effective STL 学习笔记 Item 30: 保证目标区间足够大 */--> div.org-src-container { font-size: 85%; font-family: ...
 - 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 ...
 - 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 ...
 - Effective STL 学习笔记 Item 21:Comparison Function 相关
		
Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...
 - Effective STL 学习笔记 Item 17: Swap Trick
		
假设有若干对象存于一个 vector 中: class Widget; vector<Widget> vw; 后来由于某些原因,从该容器中删除了若干对象(参考erase-remove id ...
 - Effective STL 学习笔记: Item 22 ~ 24
		
Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...
 - Effective STL 学习笔记: 多用 vector & string
		
Effective STL 学习笔记: 多用 vector & string 如果可能的话, 尽量避免自己去写动态分配的数组,转而使用 vector 和 string . 原书作者唯一想到的一 ...
 
随机推荐
- [转]Multivariate Time Series Forecasting with LSTMs in Keras
			
1. Air Pollution Forecasting In this tutorial, we are going to use the Air Quality dataset. This is ...
 - OpenCV入门指南----人脸检测
			
本篇介绍图像处理与模式识别中最热门的一个领域——人脸检测(人脸识别).人脸检测可以说是学术界的宠儿,在不少EI,SCI高级别论文都能看到它的身影.甚至很多高校学生的毕业设计都会涉及到人脸检测.当然人脸 ...
 - config配置中心之自动刷新
			
自动刷新(自动刷新是基于springcloudbus来实现的,springcloud bus是基于rabbitMQ或者Kafka来实现的) Spring Cloud Bus 将分布式的节点用轻量的消息 ...
 - python学习(21) smtp发送邮件
			
原文链接: https://www.jianshu.com/p/369ec15bfe22 本文介绍python发送邮件模块smtplib以及相关MIME模块.smtplib用于生成邮件发送的代理,发送 ...
 - 【题解】新型城市化 HAOI2017 网络流 二分图最大匹配 强连通分量
			
Prelude 好,HAOI2017终于会做一道题了! 传送到洛谷:→_→ 传送到LOJ:←_← 本篇博客链接:(●'◡'●) Solution 首先要读懂题. 考场上我是这样想的QAQ. 我们把每个 ...
 - Jenkins和Gitblit集成实现提交后自动构建
			
Gitblit是一个纯Java基于git的解决方案.它属于Apache Allura等伪造软件之一,它将票务系统与web ui结合在一起.我经历了一个设置过程,我想我可以帮助新用户避免痛点. 使用场景 ...
 - range循环
			
for i in range(10): #特殊写法,从0开始,步长为1,最大值小于10 print("loop",i) print("=========") f ...
 - R语言:克里金插值
			
基于空间自相关,R语言克里金插值 library(gstat) Warning message: In scan(file = file, what = what, sep = sep, quote ...
 - Java基础-面向对象第一特性之封装(Encapsulation)
			
Java基础-面向对象第一特性之封装(Encapsulation) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.理解什么是面向过程和面向对象 面向过程与面向对象都是我们编程中 ...
 - Codeforces Round #476 (Div. 2) [Thanks, Telegram!] C
			
http://codeforces.com/contest/965/problem/C 题目大意:n个糖,k个人,每次最多只能拿M个糖,从第一个人开始拿,可以循环D次.问Arkady最多可以拿几块糖? ...