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. 关于tab的切换之共用html页面,而引发的页面跳转错乱问题

    在一个项目中的同一个模块中,有多个tab(并且多个tab对应的页面结构完全一样),tab的每次切换,不同tab调用不同的接口,利用一个switch进行判断,根据当前的类型去调用不同的接口,返回不同数据 ...

  2. python3之es+log+date+timezone

    from dateutil.parser import parse # 使用它可以方便的将字符串解析为datetimefrom tzlocal import get_localzone # 使用它可以 ...

  3. TCP/IP网络编程系列之四(初级)

    TCP/IP网络编程系列之四-基于TCP的服务端/客户端 理解TCP和UDP 根据数据传输方式的不同,基于网络协议的套接字一般分为TCP和UDP套接字.因为TCP套接字是面向连接的,因此又称为基于流的 ...

  4. centos7 桥接配置

    cd /etc/sysconfig/network-scripts/ 名字可能各不同,一般出现在第一个位置 vim ifcfg-ens33 然后重启 systemctl restart network ...

  5. 初学者手册-Sublime Text常用快捷键

    Alt + F3 :找出当前文档中所有被划选的词语,若文档很大的话,可能会导致Sublime Text崩溃. Ctrl + kkk :删除当前行光标至行尾的所有内容. End: 光标跳至行尾. Hom ...

  6. ESXI root密码忘记,重置root密码

    今天遇到了一个叫人比较头疼的问题,早在一个月前公司拉来一台服务器,闲着没事我给装成了Esxi的虚拟机系统了,时间过久忘了当时设定的密码为何?故而翻了许久的资料,终于找好的方向,准备重置系统密码.准备搞 ...

  7. 工具类MyBatisUtils创建SqlSessionFactory

    package com.js.ai.modules.pointwall.interfac; import java.io.IOException; import java.io.InputStream ...

  8. 升级oracle 9i到10g

    从9i升级到10g两个方案可供选择:   一是利用oracle提供的一个升级实用程序dbua(database upgrade assistant)直接将9i的数据库升级到10g. 再有就是新建一个1 ...

  9. Asp.net mvc validaterequest无效的问题

    在普通的asp.net下,可以通过在页面上注明 validateRequest=“false" 这个选项来关闭请求对注入攻击的验证,但在mvc下则不行.而且,在asp.net 4.0下,哪怕 ...

  10. Docker 存储之卷(Volume)

      理解Docker(8):Docker 存储之卷(Volume) (1)Docker 安装及基本用法 (2)Docker 镜像 (3)Docker 容器的隔离性 - 使用 Linux namespa ...