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>的功能. 照抄之后发现编译不通过,有个库函数调用错误,就参考着自 ...
随机推荐
- dd装机
如何在 Linux 系统中使用 dd 命令而不会损毁你的磁盘 使用 Linux 中的 dd 工具安全.可靠地制作一个驱动器.分区和文件系统的完整镜像. _这篇文章节选自 Manning 出版社出版的图 ...
- 基于STL优先队列和邻接表的dijkstra算法
首先说下STL优先队列的局限性,那就是只提供入队.出队.取得队首元素的值的功能,而dijkstra算法的堆优化需要能够随机访问队列中某个节点(来更新源点节点的最短距离). 看似可以用vector配合m ...
- jQuery实现页面监听(某一个值发生改变时触发)
使用jQuery实现页面中监听 页面中某一个值(如input输入框)发生改变时触发. <html> <head> <title>RunJS</title& ...
- Zookeeper--配置服务
Zookeeper--配置服务 配置服务是分布式应用中重要的服务,作用是使集群中的机器可以共享配置信息中公共的部分.ZooKeeper可作为一个具有高可用,全局一致的配置服务器,允许客户端获取和更新配 ...
- 002:MySQL升级以及访问连接
目录 一. 数据库升级 1. 环境说明: 2. 环境举例: 3. 版本升级 4.关于降级问题的说明 二. MySQL的连接登录 1. 几种登录方式 2. 免密码登录 三. MySQL 参数介绍和设置 ...
- C++ 新特性-右值引用
作为最重要的一项语言特性,右值引用(rvalue references)被引入到 C++0x中.我们可以通过操作符“&&”来声明一个右值引用,原先在C++中使用“&”操作符声明 ...
- 浅谈PHP面向对象编程(七、抽象类与接口)
7.0 抽象类与接口 当定义一个类时,常常需要定义一些方法来描述该类的行为特征.但有时这些方法的实现方式是无法确定的,此时就可以使用抽象类和接口. 抽象类和接口用于提高程序的灵活性.抽象类是一种特殊的 ...
- django-bower
https://django-bower.readthedocs.io/en/latest/usage.html
- Mysql总结(二)
数据库.表.字段.行 问:查询姓黄或洪的男生分析:数据从哪来,哪个表stu条件:姓黄或洪name or and 男生gender答:select * from stu where gender=1 a ...
- python的disutils创建分发包
python中的distutils包主要用创建共享包,安装包,在平时安装python模块的时候,使用的命令如下: python setup.py install 其实以上代码就是distuitls包提 ...