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


C++ std::vector<bool>的更多相关文章

  1. c++转载系列 std::vector模板库用法介绍

    来源:http://blog.csdn.net/phoebin/article/details/3864590 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作 ...

  2. C++ 中的std::vector介绍(转)

    vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vec ...

  3. std::vector介绍

    vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vec ...

  4. std::vector<Channel2*> m_allChannels;容器,以及如何根据channelid的意义

    std::vector<Channel2*> m_allChannels;容器,以及如何根据channelid的意义 这个容器保存了所有客户端连接的channel Channel2* Li ...

  5. std::vector数据复制

    std::vector<boost::shared_ptr <ITEM> > srcItemList;  // 数据源 std::vector<ITEM>  des ...

  6. 单独删除std::vector <std::vector<string> > 的所有元素

    下面为测试代码: 1.创建 std::vector< std::vector<string> > vc2; 2.初始化 std::vector<string> vc ...

  7. std::vector的分片拷贝和插入

    一般我们在用Qt的QByteArrary或者List的时候,会有相应的append的方法,该函数,就是把数据加入末尾.但是std::vector就没有相应的方法.但是我们可以用insert方法来实现: ...

  8. 使用std::vector优化点云动画显示一例

    1. 准备 使用std::vector应该知道几点: (1)内存连续的容器,有点像数组 (2)与std::list相比,插入和删除元素比较慢- 因为数据迁移 (3)添加元素可能会引发内存分配和数据迁移 ...

  9. 随机排序std::vector,扑克牌,麻将类尤其合用

    有些需要重新对std::vector对象重新排序,特别是游戏,例如说:扑克牌,麻将,抽奖等,C++标准已经为std::vector写好了随机排序的方式,这里做个笔记: #include <alg ...

  10. (原创)动态内存管理练习 C++ std::vector<int> 模拟实现

    今天看了primer C++的 “动态内存管理类”章节,里面的例子是模拟实现std::vector<std::string>的功能. 照抄之后发现编译不通过,有个库函数调用错误,就参考着自 ...

随机推荐

  1. lbypmall虚拟主机的设置

    虚拟机配置不完整,导致访问是样式路径不正确,问题可能是config.inc.php配置不正确 1.修改/etc/php.ini 访问目录限制 open_basedir =/home/upload/:/ ...

  2. php的闭包

    闭包是指在创建时封装周围状态的函数,即使闭包所在的环境的不存在了,闭包中封装的状态依然存在. 匿名函数其实就是没有名称的函数,匿名函数可以赋值给变量,还能像其他任何PHP函数对象那样传递.不过匿名函数 ...

  3. GOF23设计模式之中介者模式(mediator)

    一.中介者模式概述 如果一个系统中对象之间的联系呈现网状结构,对象之间存在大量多对多的关系,导致关系及其复杂,这时可以引入一个中介者对象,使得各个对象只跟中介者对象打交道,从而将复杂的网络结构化为星型 ...

  4. flv 解封装

    #include <stdio.h> #include <stdlib.h> #include <string.h> #define DEBUG_INFO type ...

  5. python is 和 == 的区别

    一.is 和 == 的区别 == 比较 比较的俩边的值 is 比较 比较的是内存地址 id() 二.小数据池 数字小数据池的范围 -5 ~ 256 字符串中如果有特殊字符他们的内存地址就不一样 字符串 ...

  6. ThreadPoolExecutor之三:自定义线程池-扩展示例

    ThreadPoolExecutor是可扩展的,下面一个示例: package com.dxz.threadpool.demo1; import java.util.concurrent.Blocki ...

  7. 【语音识别】Microsoft Speech Platform 自学笔记2 环境要求与安装过程

    笔记人:又吹风 时 间:2012/12/16 主要内容:Microsoft Speech Platform的环境要求与安装过程. 上次也说过了,当前Microsoft Speech Platform最 ...

  8. XStream使用笔记

    XStream是一个可以将JavaBean生成XML的工具,很方便的帮助我们在后台服务器将数据序列化为XML,接下来就可以将XML数据响应给前台进行数据交互 XStream需要的jar包 * 核心JA ...

  9. Running command-line BLAST

    Ubuntu安装BLAST 2014-02-09 10:45:03|  分类: Linux/Ubuntu|举报|字号 订阅     下载LOFTER我的照片书  |     very easy! su ...

  10. 根据不同分辨率载入相应CSS样式表

    index.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:// ...