一、定义
位图法就是bitmap的缩写。所谓bitmap,就是用每一位来存放某种状态,适用于大规模数据,但数据状态又不是很多的情况。通常是用来判断某个数据存不存在的。在STL中有一个bitset容器,引用bitset介绍:
A bitset is a special container class that is designed to store bits (elements with only two possible values: 0 or 1,true or false, …)。The class is very similar to a regular array, but optimizing for space allocation: each element occupies only one bit (which is eight times less than the smallest elemental type in C++: char)。Each element (each bit) can be accessed individually: for example, for a given bitset named mybitset, the expression mybitset[3] accesses its fourth bit, just like a regular array accesses its elements.
二、数据结构
unsigned int bit[N];
在这个数组里面,可以存储 N * sizeof(int)个数据,但是最大的数只能是N * sizeof(int) - 1.假如,我们要存储的数据范围为0-15,则我们只需要使得N=1,这样就可以把数据存进去。如下图:

数据为【5,1,7,15,0,4,6,10】,则存入这个结构中的情况为


位图法(Bitmap)是一种高效的数据存储方式,尤其适用于大规模数据且状态有限的情况。它利用bitset容器存储数据,每个元素仅占用1位。本文介绍了位图法的数据结构、写入数据的操作、读取指定位的方法,以及位图法的缺点和应用场景,如判断整数是否存在、查找数组重复元素等。
最低0.47元/天 解锁文章
4306

被折叠的 条评论
为什么被折叠?



