vector<bool> 看起来像是一个存放布尔变量的容器,可是其实自己其实并非一个容器,它里面存放的对象也不是布尔变量,这一点在 GCC 源码中 vector<bool> 的注释中写的很清楚:java
/** * @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++
若是 c 是支持 [ ] 操做的 T 类型的容器,那么下面的表达式必须能够编译:sql
\(T* p = \&c[0]\)bash
但一样是按照 C++ 标准, vector<bool> 做为一个单独的对象,它里面存放的并不是真正的 bool, 为了节省内存,其中存放的是 bitfields ,它用其中的每个 bit 来表示 bool 。 不一样于普通的 [ ] 操做符, vector<bool>::[ ] 返回并非 bool& ,而是一个能够将 vector<bool> 的内部表示转换成 bool 类型的 proxy ,因此表达式:ide
vector<bool> c; bool* p = &c[0];
不能编译。post
因此,用到 vector<bool> 的时候,切记它并不是真正的容器,若是想用真正的存放 bool 类型的容器,需使用 deque 。ui