vector <bool>及其相关

vector<bool>是std::vector的特化版本,用于节省空间,通过位存储bool值。虽然节省空间,但可能降低访问效率,因为它不是真正意义上的容器,不遵循C++标准对容器的要求。使用时需要注意其时间效率和模板代码的兼容性问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

std::vector<bool>

template < class T, class Alloc = allocator<T> > class vector; // generic template
template <class Alloc> class vector<bool,Alloc>;               // bool specialization

Vector of bool

This is a specialized version of vector, which is used for elements of type bool and optimizes for space.

It behaves like the unspecialized version of vector, with the following changes:

  • The storage is not necessarily an array of bool values, but the library implementation may optimize storage so that each value is stored in a single bit.

     
  • Elements are not constructed using the allocator object, but their value is directly set on the proper bit in the internal storage.

     
  • Member function flip and a new signature for member swap.

     
  • A special member type, reference, a class that accesses individual bits in the container's internal storage with an interface that emulates a bool reference. Conversely, member type const_reference is a plain bool.
     
  • The pointer and iterator types used by the container are not necessarily neither pointers nor conforming iterators, although they shall simulate most of their expected behavior.

 

上面是vector <bool>的标准文档节选,其中值得注意的有以下几部分

  • vector <bool>是为了当元素都为bool类型时,优化空间所产生的vector<T>的特化版,主要解决的就是节省空间的问题

       节省空间的方法是,按照一个二进制位来存储,而不是一个字节。一个字节可以存八个bool值,极大的节省了空间,而且vector <bool>并没有要求底层的存储必须是连续的空间。

  • vector <bool>提高空间效率的同时降低了时间效率

       因为它是按位存的,一个字节存八个bool值,但是要知道第i个bool值的真假,需要进行位运算,效率就会低一些,存的bool值越多花费的时间就越长,这是典型的以时间换空间的策略。

  • vector <bool>不是一个容器,不是一个容器,不是一个容器

       如果一个对象是容器,它必须满足C++标准中列出的全部条件,其中一个是,假设c是包括对象T的容器,并且c支持operator[],那么以下的代码必须可以被编译:T *p = &c[0];正常的vector容器会返回一个指向该对象的指针,我们能够创建一个指向bool的指针,而指向单个位的指针则是不同意的。指向单个位的引用也是被禁止的,这使得在设计vector<bool>的接口时产生了一个问题。由于vector<T>::operator[]的返回值应该是T&.假设vector<bool>中所存储的确实是bool。那么这就不是问题。但由于实际上并不是如此,所以vector<bool>::operator[]须要返回一个指向一个单个位的引用,而这种引用并不存在。

如上所述,并不建议使用vector <bool>的原因有1)它的时间效率低,性能会有问题 2)它并不是个真正的容器,因此在模版代码上的使用可能会有问题

 

说到关于位的问题,在这里再提一下bitmap,bit是位,map是图,bitmap就是位图,准确的讲是基于位的映射。Bitmap就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素。由于采用了Bit为单位来存储数据,因此在存储空间方面,可以大大节省。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值