标准非STL容器 : bitset
1. 概念
什么是“标准非STL容器”?标准非STL容器是指“可以认为它们是容器,但是他们并不满足STL容器的所有要求”。前文提到的容器适配器stack、queue及priority_queue都是标准非STL容器的一部分。此外,valarray也是标准非STL容器。
bitset:一种高效位集合操作容器。
2. API
bitset提供的api:
(constructor) Construct bitset (public member function)
operator[] Access bit (public member function)
set Set bits (public member function)
reset Reset bits (public member function )
flip Flip bits (public member function)
to_ulong Convert to unsigned long integer (public member function)
to_string Convert to string (public member function)
count Count bits set (public member function)
size Return size (public member function)
test Return bit value (public member function )
any Test if any bit is set (public member function)
none Test if no bit is set (public member function)
3. 源码剖析
SGI bitset部分实现源码
- template<size_t _Nb>
- class bitset : private _Base_bitset<__BITSET_WORDS(_Nb)>
- {
- private:
- typedef _Base_bitset<__BITSET_WORDS(_Nb)> _Base;
- typedef unsigned long _WordT;
- private:
- void _M_do_sanitize() {
- _Sanitize<_Nb%__BITS_PER_WORD>::_M_do_sanitize(this->_M_hiword());
- }
- .....
- }
- #define __BITS_PER_WORD (CHAR_BIT*sizeof(unsigned long))
- #define __BITSET_WORDS(__n) \
- ((__n) < 1 ? 1 : ((__n) + __BITS_PER_WORD - 1)/__BITS_PER_WORD)
- template<size_t _Nw>
- struct _Base_bitset {
- typedef unsigned long _WordT;
- _WordT _M_w[_Nw]; // 0 is the least significant word.
- _Base_bitset( void ) { _M_do_reset(); }
- _Base_bitset(unsigned long __val) {
- _M_do_reset();
- _M_w[0] = __val;
- }
- static size_t _S_whichword( size_t __pos )
- { return __pos / __BITS_PER_WORD; }
- static size_t _S_whichbyte( size_t __pos )
- { return (__pos % __BITS_PER_WORD) / CHAR_BIT; }
- static size_t _S_whichbit( size_t __pos )
- { return __pos % __BITS_PER_WORD; }
- static _WordT _S_maskbit( size_t __pos )
- { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
- _WordT& _M_getword(size_t __pos) { return _M_w[_S_whichword(__pos)]; }
- _WordT _M_getword(size_t __pos) const { return _M_w[_S_whichword(__pos)]; }
- _WordT& _M_hiword() { return _M_w[_Nw - 1]; }
- _WordT _M_hiword() const { return _M_w[_Nw - 1]; }
- void _M_do_and(const _Base_bitset<_Nw>& __x) {
- for ( size_t __i = 0; __i < _Nw; __i++ ) {
- _M_w[__i] &= __x._M_w[__i];
- }
- }
- void _M_do_or(const _Base_bitset<_Nw>& __x) {
- for ( size_t __i = 0; __i < _Nw; __i++ ) {
- _M_w[__i] |= __x._M_w[__i];
- }
- }
- void _M_do_xor(const _Base_bitset<_Nw>& __x) {
- for ( size_t __i = 0; __i < _Nw; __i++ ) {
- _M_w[__i] ^= __x._M_w[__i];
- }
- }
节选上述代码,可以得到:
1. bitset继承_Base_bitset,具体操作封装在_Base_bitset中
2. bitset 的size作为模板参数(非类型模板参数的一个要求是,编译器能在编译期就能把参数确定下来),因此,bitset大小在编译期固定,不支持插入和删除元素
3. 各种位操作,性能高
4._Base_bitset使unsigned long作为底层存储,不支持指针、引用、迭代器
5. 使用 _WordT _M_w[_Nw];分配内存,因此在栈中定义bitset需要注意大小(和STL标准容器堆内存分配区别开)。
eg,下面的代码将栈溢出(测试机器栈内存10M)
- void fun()
- {
- const int n = 800000000;
- bitset<n> a;
- cout << a.size() << endl;
- }
- int main(int argc, char** argv)
- {
- fun();
- return 0;
- }
大内存分配可以分配在堆中,如下:
- const int n = 800000000;
- bitset<n> *a = new(std::nothrow) bitset<n>;
- if(a)
- {
- cout << a->size() << endl;
- delete a;
- a = NULL;
- }
4. vector<bool>及deque<bool>
bitset高效,但是size必须在编译器确定,不支持插入和删除。因此,一个可能的替代品是vector<bool>和deque<bool>
两者的区别:
vector<bool>不是一个STL容器,并且不容纳bool(like bitse底层t机制)
deque<bool>是一个STL容器,它保存真正的bool值
分别运行
- deque<bool> a;
- a[0] = 0;
- bool* b = &a[0];
- cout << *b << endl;
和
- vector<bool> a;
- a[0] = 0;
- bool* b = &a[0];
- cout << *b << endl;
将会发现:
使用deque<bool>正确,而是用vector<bool>会报错:“cannot convert `std::_Bit_reference*' to `bool*' in initialization“
但是,deque简直是在践踏内存。
使用deque<bool>
- int main(int argc, char** argv)
- {
- deque<bool> a(10000000000);
- sleep(100);
- return 0;
- }
内存使用:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
23612 work 25 0 9990m 9.8g
720 S 0.0 65.0 0:39.35 test
使用vector<bool>
- int main(int argc, char** argv)
- {
- vector<bool> a(10000000000);
- sleep(100);
- return 0;
- }
内存使用:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
23909 work 25 0 1198m 1.2g
716 S 0.0 7.8 0:01.31 test
使用bitset
- int main(int argc, char** argv)
- {
- const unsigned long int n = 10000000000;
- bitset<n> *a = new(std::nothrow) bitset<n>;
- sleep(100);
- return 0;
- }
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
24439 work 25 0 1198m 1.2g
712 S 30.7 7.8 0:00.92 test
10亿个bool,vector<bool>和bitset使用内存1198M,deque<bool>则是9990M
5. 总结
在需要对位集合进行操作的时候,如何操作集合大小比较固定,优先选择高效的bitset;
如果需要动态增删元素,或者编译期间无法确定集合大小,则可以考虑vector<bool>,deque<bool>内存开销太大,基本上不考虑。
参考:
http://www.sgi.com/tech/stl/download.html
http://www.cplusplus.com/reference/stl/vector/
http://www.cplusplus.com/reference/stl/bitset/
扩展阅读:
Vector specialization: vector<bool>
The vector class template has a special template specialization for the bool type.
This specialization is provided to optimize for space allocation: In this template specialization, each element occupies only one bit (which is eight times less than the
smallest type in C++:char).
The references to elements of a bool vector
returned by the vector members are not references tobool objects,
but a special member type which is a reference to a single bit, defined inside thevector<bool> class
specialization as:
- class vector<bool>::reference {
- friend class vector;
- reference(); // no public constructor
- public:
- ~reference();
- operator bool () const; // convert to bool
- reference& operator= ( const bool x ); // assign from bool
- reference& operator= ( const reference& x ); // assign from bit
- void flip(); // flip bit value.
- }
标准非STL容器 : bitset的更多相关文章
- 标准非STL之bitset
template <size_t N> class bitset; BitsetA bitset stores bits (elements with only two possible ...
- STL容器 -- Bitset
核心内容:Bitset 是 STL 中的二进制容器, 存放的时 bit 位元素, 每一位只占一个 bit 位, 取值 0 或者 1, 可以像整形元素一样按位与或非, 并且大大优化了时间和空间复杂度. ...
- STL容器总结
一. 种类: 标准STL序列容器:vector.string.deque和list. 标准STL关联容器:set.multiset.map和multimap. 非标准序列容器slist和rope.sl ...
- STL容器与配接器
STL容器包括顺序容器.关联容器.无序关联容器 STL配接器包括容器配接器.函数配接器 顺序容器: vector 行为类似于数组,但可以根据要求 ...
- STL容器的本质
http://blog.sina.com.cn/s/blog_4d3a41f40100eof0.html 最近在学习unordered_map里面的散列函数和相等函数怎么写.学习过程中看到了一个好帖子 ...
- STL容器底层数据结构的实现
C++ STL 的实现: 1.vector 底层数据结构为数组 ,支持快速随机访问 2.list 底层数据结构为双向链表,支持快速增删 3.deque ...
- [技术] OIer的C++标准库 : STL入门
注: 本文主要摘取STL在OI中的常用技巧应用, 所以可能会重点说明容器部分和算法部分, 且不会讨论所有支持的函数/操作并主要讨论 C++11 前支持的特性. 如果需要详细完整的介绍请自行查阅标准文档 ...
- Sword STL容器分类介绍
标准STL序列容器:vector.string.deque和list. 标准STL关联容器:set.multiset.map和multimap. 非标准序列容器slist和rope.slist是一个单 ...
- STL 容器的概念
STL 容器的概念 在实际的开发过程中,数据结构本身的重要性不会逊于操作于数据结构的算法的重要性,当程序中存在着对时间要求很高的部分时,数据结构的选择就显得更加重要. 经典的数据结构数量有限,但是我们 ...
随机推荐
- 配置 JAVA 环境 JDK + IDEA
配置JDK 搜索 ORACLE 官网,找到 JDK,下载 JDK8 版本 / JDK11 版本 选择合适的路径,我这里放在了 D 盘 配置下方系统环境变量,变量名为 JAVA_HOME,把刚刚安装的J ...
- 计算机网络之网络层路由选择协议(自治系统AS、RIP、OSPF、BGP)
文章转自:https://blog.csdn.net/weixin_43914604/article/details/105313629 学习课程:<2019王道考研计算机网络> 学习目的 ...
- 【TLS】-TLS/SSL笔记
目录 前言 概念 对称加密 非对称加密 公钥 单向加密 数字签名 基础 作用 SSL/TLS 模型 运作 问题&解答 基本过程 握手阶段 客户端发出请求(ClientHello) 服务器回应( ...
- cf Learn from Life (简单贪心)
有N个人站在一楼.一个电梯最多承载K个人. 每个人都有一个想去的楼层.f[1]....f[N]. f[i]属于[2,2000] 从a层到b层需花费abs(a-b)秒. 问电梯送完所有人然后回到一楼至少 ...
- Vue3学习(十)之 页面、菜单、路由的使用
一.前言 好几天没更文了,周末真的太冷了,在家躺了一天不爱动.今天给暖气了,相对不那么冷了,就可以继续更文了. 由文章标题不难看出,就是实现点击菜单跳转的意思,我写的很直白了,哈哈. 二.实现点击菜单 ...
- 使用.NET6打造动态API
ApiLite是直接将Service层自动生成api路由,可以不用添加Controller,支持模块插件化,在项目开发中能够提高工作效率,降低代码量. 开发环境 .NET SDK 6.0.100-rc ...
- 关于dns服务工作的原理,和配置的细节理解。
dns服务器相关 1,dns原理,也就是迭代,和递归查询.将域名解析为ip的过程. 一次完整的查询请求经过的流程: Client -->hosts文件 -->DNS Service Loc ...
- Filter学习笔记
博客园的编辑器太丑了,所以我换用了别的Markdown编辑器,并用图片形式上传.
- git远程仓库、提交代码操作
初始化仓库 1.初始化 git init #或 git clone 远程仓库地址 git init 后续要添加远程仓库,git clone不需要再添加 2.连接仓库 git remote add 远程 ...
- Jquery的常用使用方法
1.获取单个checkbox选中项(三种写法)$("input:checkbox:checked").val()或者$("input:[type='checkbox']: ...