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. jeecg中service中注入jdbc的注解

    @Resource private JdbcTemplate jdbcTemplate;

  2. C语言的补码表示和unsigned及signed的转换

    这东西实际编程时一直无视的,范围小了就换个大点的表示形式,但是总觉得基础知识还是掌握得好,免得到时候用移位运算或类型转换或笔试题时要花时间想. C语言的基本类型有char.int.float.doub ...

  3. 关于FPGA供电

    FPGA是一种多电源需求的芯片,主要有3种电源需求: VCCINT:核心工作电压,PCI Express (PCIe) 硬核IP 模块和收发器物理编码子层(PCS) 电源.一般电压都很低,目前常用的F ...

  4. (转)jdbc 调用 sql server 的存储过程时“该语句没有返回结果集”的解决方法

    本文转载自:http://hedyn.iteye.com/blog/856040 在JDBC中调用SQL Server中的存储过程时出现如下异常: com.microsoft.sqlserver.jd ...

  5. python--logging库学习_第三波

    本文介绍如何写一个Python日志类,用来输出不同级别的日志信息到本地文件夹下的日志文件里.为什么需要日志输出呢,我们需要记录我们测试脚本到底做了什么事情,最好的办法是写事件监听.这个事件监听,对我们 ...

  6. POJ 3061 Subsequence(尺取法)

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18145   Accepted: 7751 Desc ...

  7. Java-Runoob-高级教程:Java 文档注释

    ylbtech-Java-Runoob-高级教程:Java 文档注释 1.返回顶部 1. Java 文档注释 Java 支持三种注释方式.前两种分别是 // 和 /* */,第三种被称作说明注释,它以 ...

  8. 小程序picker组件当range是objectArray时的使用

  9. [置顶] ubuntu版本很老,apt-get update更新失败时(W: Failed to fetch ...)------如何创建新的sources.list

    在说这个解决方案之前,我先说下,目前遇到的问题: 我使用 sudo apt-get update 之后,更新失败.具体原因如下: W: Failed to fetch http://cn.archiv ...

  10. mysql 里的 ibdata1 文件不断的增长

    我们在 Percona 支持栏目经常收到关于 MySQL 的 ibdata1 文件的这个问题.当监控服务器发送一个关于 MySQL 服务器存储的报警时,恐慌就开始了 —— 就是说磁盘快要满了.一番调查 ...