STL:vector<bool> 和bitset
今天某个地方要用到很多位标记于是想着可以用下bitset,不过发现居然是编译时确定空间的,不能动态分配。那就只能用vector来代替一下了,不过发现居然有vector<bool>这个特化模板,按照说明它的空间分配一般的实现就是一个元素一个bit,这就和bitset具有类似的空间效率了。另外它支持flip和一个具有不同签名的swap函数,前者将容器内所有值翻转,后者可以交换独立元素。
参考:
http://www.cplusplus.com/reference/vector/vector-bool/
std::vector<bool>
template < class T, class Alloc = allocator<T> > class vector; // generic template
template <class Alloc> class vector<bool,Alloc>; // bool specialization
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 (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.
STL:vector<bool> 和bitset的更多相关文章
- Effective STL 学习笔记 Item 18: 慎用 vector<bool>
vector<bool> 看起来像是一个存放布尔变量的容器,但是其实本身其实并不是一个容器,它里面存放的对象也不是布尔变量,这一点在 GCC 源码中 vector<bool> ...
- 《条目十八》避免使用vector<bool>
<条目十八>避免使用vector 先说结论: 一是:vector<bool>不是标准容器,因为标准容器的对于T *p = &c[0];必须是可编译的. 二是:vecto ...
- 【C++】朝花夕拾——STL vector
STL之vector篇 N久之前是拿C的数组实现过vector中的一些简单功能,什么深拷贝.增删查找之类的,以为vector的实现也就是这样了,现在想想真是...too young too naive ...
- C++ STL vector容器学习
STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器.迭代器.函数对象和算法的模板.其中容器是存储类型相同的数据的结构(如vector, ...
- STL vector
STL vector vector是线性容器,它的元素严格的按照线性序列排序,和动态数组很相似,和数组一样,它的元素存储在一块连续的存储空间中,这也意味着我们不仅可以使用迭代器(iterator)访问 ...
- STL vector用法介绍
STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和f ...
- STL vector+sort排序和multiset/multimap排序比较
由 www.169it.com 搜集整理 在C++的STL库中,要实现排序可以通过将所有元素保存到vector中,然后通过sort算法来排序,也可以通过multimap实现在插入元素的时候进行排序.在 ...
- STL vector 用法介绍
介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...
- STL vector使用方法介绍
介绍 这篇文章的目的是为了介绍std::vector,怎样恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...
随机推荐
- webservice gsoap 小记
参考 http://www.cs.fsu.edu/~engelen/soap.html 1. web service client application > wsdl2h -s -o MyHe ...
- 运行安装mysql 报错 [root@localhost mysql-mult]# ./scripts/mysql_install_db --defaults-file=conf/3306my.cnf FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_
运行安装mysql 报错 [root@localhost mysql-mult]# ./scripts/mysql_install_db --defaults-file=conf/3306my.cn ...
- 解决Mysql Workbench的Error Code: 1175错误 无法删除数据
使用workbench,如果你要批量更新或删除数据,一般会报“ Error Code: 1175 You are using safe update mode and you tried to upd ...
- Linux开发端口的基本操作命令
Linux端口操作命令 查看开放端口:firewall-cmd --list-all 开发8080端口 --permanent 代码永久开发:firewall-cmd --add-port=8080/ ...
- (转)python高级:列表解析和生成表达式
一.语法糖的概念 “糖”,可以理解为简单.简洁,“语法糖”使我们可以更加简洁.快速的实现这些功能. 只是Python解释器会把这些特定格式的语法翻译成原本那样复杂的代码逻辑 我们使用的语法糖有: if ...
- 四、CLR执行程序集中代码和IL代码简介
三.加载公共语言运行时中介绍了在安装了.Net Framework中加载公共语言运行时,公共语言运行时加载程序集的过程.以及通过vs stdio设置源码编译的目标平台的过程. 本问主要介绍公共语言加载 ...
- python实现二叉树的建立以及遍历(递归前序、中序、后序遍历,队栈前序、中序、后序、层次遍历)
#-*- coding:utf-8 -*- class Node: def __init__(self,data): self.data=data self.lchild=None self.rchi ...
- SQL查询排名第二名的信息
今天看见同学去面试的面试题,查询出某个字段排名第二名的信息,自己就看看 如果是Oracle ,这不就是考察Oracle分页么,以Oracle的emp表为例,根据薪水排名,查询排名第二的员工信息: se ...
- Python 基础语法——数字和表达式(包含数学上的一些函数)
>>> 2+2 4 >>> 1/2 0 >>> 1.0/2.0 0.5 >>> 1/2.0 0.5 >>> 1 ...
- 关于注解Annotation第二篇
写一个注解使用类,如下: public class Test { @Code(author = "mazhi",date="20170611") private ...