std::vector

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.

These changes provide a quirky interface to this specialization and favor memory optimization over processing (which may or may not suit your needs). In any case, it is not possible to instantiate the unspecialized template of vector for bool directly. Workarounds to avoid this range from using a different type (char, unsigned char) or container (like deque) to use wrapper types or further specialize for specific allocator types.

bitset is a class that provides a similar functionality for fixed-size arrays of bits.

Member functions

  • flip: Flip bits (public member function )
  • swap: Swap containers or elements (public member function )

Non-member class specializations

  • hash<vector>: Hash for vector (class template specialization )

Data races

Simultaneous access to different elements is not guaranteed to be thread-safe (as storage bytes may be shared by multiple bits).

Example

#include <iostream>
#include <vector> using namespace std; int main(int argc, char **argv)
{
vector<bool> flags; flags.push_back(true);
flags.push_back(false);
flags.push_back(false);
flags.push_back(true); flags.flip(); // false true true false cout << "flags value:";
for(int i=0; i < flags.size(); i++)
cout << " " << flags.at(i); cout << "\n";
return 0;
}

Reference

cplusplus


最新文章

  1. 何必苦等VS2015?来看看VS2013下实现移动端的跨平台开发
  2. 黑马程序员——OC语言Foundation框架 (2) NSArray NSSet NSDictionary\NSMutableDictionary
  3. Interproscan, xml文件转化为tsv
  4. 关于volatile和synchronized
  5. java 13-3 int类型的包装包Integer
  6. [置顶] 【Git入门之十四】Git GUI
  7. PHP 18:data_valid_fns.php 看正则表达式
  8. 基于 Aliexpress API 的小程序 : 批量 Copy 产品到不同的店铺
  9. 《SQL必知必会》学习笔记二)
  10. python 内存NoSQL数据库
  11. asp.net core系列 55 IS4结合Identity密码保护API
  12. ES进阶--04
  13. rviz初接触2.0
  14. win10系统安装两个版本的python,该怎么安装Django
  15. 洛谷P1445 樱花
  16. tomcat jvm 参数优化
  17. 翻译Java虚拟机的结构
  18. 搭建Linux-java web运行环境之一:安装jdk+tomcat
  19. python图片处理(三)
  20. web框架们~Django~Flask~Tornado

热门文章

  1. bzoj3258秘密任务
  2. Linux 解压 压缩 tar
  3. Error[Li006]: duplicate definitions for &quot;******&quot;
  4. LogStation 支持浏览器实时查看日志
  5. DS04--树
  6. python 数组的操作--统计某个元素在列表中出现的次数
  7. css ! important 兼容性的一点测试
  8. 硬盘IOPS与读写速度
  9. svn关键词BASE, HEAD, COMMITTED, PREV的深入理解
  10. 关于jquery中的parent的认定