C++ std::vector<bool>
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
C++ std::vector<bool>的更多相关文章
- c++转载系列 std::vector模板库用法介绍
来源:http://blog.csdn.net/phoebin/article/details/3864590 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作 ...
- C++ 中的std::vector介绍(转)
vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vec ...
- std::vector介绍
vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vec ...
- std::vector<Channel2*> m_allChannels;容器,以及如何根据channelid的意义
std::vector<Channel2*> m_allChannels;容器,以及如何根据channelid的意义 这个容器保存了所有客户端连接的channel Channel2* Li ...
- std::vector数据复制
std::vector<boost::shared_ptr <ITEM> > srcItemList; // 数据源 std::vector<ITEM> des ...
- 单独删除std::vector <std::vector<string> > 的所有元素
下面为测试代码: 1.创建 std::vector< std::vector<string> > vc2; 2.初始化 std::vector<string> vc ...
- std::vector的分片拷贝和插入
一般我们在用Qt的QByteArrary或者List的时候,会有相应的append的方法,该函数,就是把数据加入末尾.但是std::vector就没有相应的方法.但是我们可以用insert方法来实现: ...
- 使用std::vector优化点云动画显示一例
1. 准备 使用std::vector应该知道几点: (1)内存连续的容器,有点像数组 (2)与std::list相比,插入和删除元素比较慢- 因为数据迁移 (3)添加元素可能会引发内存分配和数据迁移 ...
- 随机排序std::vector,扑克牌,麻将类尤其合用
有些需要重新对std::vector对象重新排序,特别是游戏,例如说:扑克牌,麻将,抽奖等,C++标准已经为std::vector写好了随机排序的方式,这里做个笔记: #include <alg ...
- (原创)动态内存管理练习 C++ std::vector<int> 模拟实现
今天看了primer C++的 “动态内存管理类”章节,里面的例子是模拟实现std::vector<std::string>的功能. 照抄之后发现编译不通过,有个库函数调用错误,就参考着自 ...
随机推荐
- ecmall公告挂件分析(转)--此挂件写法已有更新的写法。
ecmall的首页,基本上都是由挂件的形式实现的.ecmall所有的挂件程序,都在external\widgets文件下面.ecmall首页公告的插件,就是notice目录里面. 分析里面文件,con ...
- 解决openoffice进程异常退出的办法:
实现以守护进程,定时检测openoffice是否退出,如果进程不存在,通过脚本将openoffice起起来即可. 具体操作步骤: 第一步: 将openoffice.sh脚本放置在root目录下面, ...
- ibernate+Struts2环境如何使用jqGrid。
因为公司项目需要,在Hibernate+Struts2的环境下,研究了一下如何使用jqGrid. 说实在的,Struts2+jqGrid不是一个很好的组合.因为jqGrid中很多功能,基本上都使用的是 ...
- 【转】JMeter-Java Sampler编写范例
今天,听段念的培训,学了一下Jmeter写Java请求的过程. 根据某博文,修改其中代码的bug后,贴在下面吧:另外公司一同学Raylupas在半年前也写过关于JMeter写Java Sampler的 ...
- java代码------计算器核心位置添加
总结:点击等号时,什么代码 else if(str.equals("-")){ ready=true; if(c=='\0'){ num1=Double.parseDouble(j ...
- 20165226 2017-2018-4 《Java程序设计》第8周学习总结
20165226 2017-2018-4 <Java程序设计>第8周学习总结 教材学习内容总结 第十二章 创建线程的方式有三种,分别是: - 继承Thread类创建线程,程序中如果想要获取 ...
- Java-Runoob-高级课程:Java 集合框架
ylbtech-Java-Runoob-高级课程:Java 集合框架 1.返回顶部 1. Java 集合框架 早在 Java 2 中之前,Java 就提供了特设类.比如:Dictionary, Vec ...
- [置顶]
C语言中 || 和 &&
|| 或操作,|| 为界将表达式分为两部分,他会先算前一部分,如果前一部分为真,他将停止运算,如果为假,他才会算第二部分,你这里第一部分就为真了,第二部分当然也就不会算了. 例如: a || b , ...
- Cinder Backup备份
cinder 备份提供了三种驱动服务: Ceph,TSM,Swift 其中默认备份驱动服务为swift cinder 驱动服务的配置在cinder.conf文件中 backup_driver=cind ...
- azkaban平台的使用
最近接触一些大数据的测试,有些hadoop/spark任务在服务器测试不太方便,会放到azkaban上跑 简单写下azkaband的使用流程:包括任务的上传和提交任务到hadoop集群 一 登陆azk ...