【c++】bitmap的实现

@最近被问到怎么在很多数据(如20亿)中实现快速查找,没有答上来…

@假设是用unordered_map来实现,查找效率为O(N),假设元素为int类型,则需要20亿*4Bytes,这显然太占资源

@后来了解到bitmap,也就是所谓的位图,用一两bit表示该数是否出现过,例如int为8字节,32位,则可存进0-31的数,则20亿数需要20亿/32Bytes,这样便大大节约了资源

@实现代码如下,主要实现其添加、删除与查找,通过位运算实现:
1.如实现查找功能,则通过int_/32可知该数应该属于第几组,通过int_%32可知该数在该组的下标树,通过与1<<location则可知查找的该位置是否为1判断该数是否存在
2.添加与删除功能原理类似,不再赘述…

class my_bitmap {
public:
	my_bitmap(){
		memset(a, 0, sizeof(a));
	};
	//添加
	void insert(const int& int_) {
		int arr = int_ / 32;
		int location = int_ % 32;
		a[arr] |= (1<<location);
	}
	//删除
	void erase(const int& int_) {
		int arr = int_ / 32;
		int location = int_ % 32;
		a[arr] &= ~(1<<location);
	}
	//查找
	void find(const int& int_) {
		int arr = int_ / 32;
		int location = int_ % 32;
		if (a[arr] & 1 << location) {
			cout << "is find!" << endl;
		}
		else cout << "is not find!" << endl;
	}
private:
	int a[100];//可以存100*32个数
};

@代码若有误,欢迎指正!!!
@理解若有误,欢迎指正!!!
@bitmap还能够实现快速排序、快速去重,有兴趣的可以自己了解下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个用于C++ MFC开发Bitmap图片操作类,在文件中叫CBitmapEx,可用于放大,缩小,翻转,过渡和其他有用的功能,有兴趣的朋友可以下载看看。 部分public method: // // void Create(long width, long height); // void Create(CBitmapEx& bitmapEx); // void Load(LPTSTR lpszBitmapFile); // void Save(LPTSTR lpszBitmapFile); // void Scale(long horizontalPercent=100, long verticalPercent=100); // void Rotate(long degrees=0, _PIXEL bgColor=_RGB(0,0,0)); // void FlipHorizontal(); // void FlipVertical(); // void MirrorLeft(); // void MirrorRight(); // void MirrorTop(); // void MirrorBottom(); // void Clear(_PIXEL clearColor=_RGB(0,0,0)); // void Negative(); // void Grayscale(); // void Sepia(long depth=34); // void Emboss(); // void Engrave(); // void Pixelize(long size=4); // void Draw(HDC hDC); // void Draw(long dstX, long dstY, long width, long height, // CBitmapEx& bitmapEx, long srcX, long srcY); // void Draw(long dstX, long dstY, long width, long height, // CBitmapEx& bitmapEx, long srcX, long srcY, long alpha); // void Draw(long dstX, long dstY, long dstWidth, long dstHeight, // CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight); // void Draw(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, // long srcX, long srcY, long srcWidth, long srcHeight, long alpha); // void DrawTransparent(long dstX, long dstY, long width, long height, // CBitmapEx& bitmapEx, long srcX, long srcY, _PIXEL transparentColor=_RGB(0,0,0)); // void DrawTransparent(long dstX, long dstY, long width, long height, // CBitmapEx& bitmapEx, long srcX, long srcY, long alpha, // _PIXEL transparentColor=_RGB(0,0,0)); // void DrawTransparent(long dstX, long dstY, long dstWidth, long dstHeight, // CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, // _PIXEL transparentColor=_RGB(0,0,0)); // void DrawTransparent(long dstX, long dstY, long dstWidth, long dstHeight, // CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, // long alpha, _PIXEL transparentColor=_RGB(0,0,0)); // LPBI

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值