zoukankan      html  css  js  c++  java
  • STL:vector<bool> 和bitset

    今天某个地方要用到很多位标记于是想着可以用下bitset,不过发现居然是编译时确定空间的,不能动态分配。那就只能用vector来代替一下了,不过发现居然有vector<bool>这个特化模板,按照说明它的空间分配一般的实现就是一个元素一个bit,这就和bitset具有类似的空间效率了。另外它支持flip和一个具有不同签名的swap函数,前者将容器内所有值翻转,后者可以交换独立元素。

    参考:

    http://www.cplusplus.com/reference/vector/vector-bool/

    class template specialization
    <vector>

    std::vector<bool>

    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 ofvector for bool directly. Workarounds to avoid this range from using a different type (charunsigned 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.

  • 相关阅读:
    hdu2222 AC自动机入门
    bzoj1095: [ZJOI2007]Hide 捉迷藏 动态点分治学习
    【NOI2014】起床困难综合症 贪心
    bzoj1822: [JSOI2010]Frozen Nova 冷冻波网络流
    洛谷3767 膜法 带权并查集+分治
    NOI2015品酒大会 后缀数组
    NOI2015程序自动分析 并查集
    NOI2015软件包管理器 树剖线段树
    51nod1244 欧拉函数之和 杜教筛
    51nod1244 莫比乌斯函数之和 杜教筛
  • 原文地址:https://www.cnblogs.com/lailailai/p/3663199.html
Copyright © 2011-2022 走看看