首页 > 编程语言 > 详细

C++实现位图

时间:2016-06-07 13:13:45      阅读:346      评论:0      收藏:0      [点我收藏+]
#pragma once

#include <vector>

class BitMap
{
public:
	BitMap()
		:_size(0)
	{}

	void Resize(size_t size)
	{
		_a.resize((size>>5) + 1);
	}

	void Set(size_t x)
	{
		size_t index = x>>5;
		size_t n = x % 32;

		_a[index] |= (1<<n);

		++_size;
	}

	void Reset(size_t x)
	{
		size_t index = x>>5;
		size_t n = x % 32;

		_a[index] &= (~(1<<n));

		--_size;
	}

	bool Test(size_t x)
	{
		size_t index = x>>5;
		size_t n = x % 32;

		return _a[index] & (1<<n);
	}

	size_t Size()
	{
		return _size;
	}

protected:
	vector<size_t> _a;
	size_t _size;
};


void BitMapTest()
{
	BitMap bitmap;
	bitmap.Resize(65);
	bitmap.Set(65);
	bitmap.Set(1);
	bitmap.Set(4);
	bitmap.Set(45);

	cout<<"65?"<<bitmap.Test(65)<<endl;
	cout<<"1?"<<bitmap.Test(1)<<endl;
	cout<<"4?"<<bitmap.Test(4)<<endl;
	cout<<"45?"<<bitmap.Test(45)<<endl;
	cout<<"25?"<<bitmap.Test(25)<<endl;

	cout<<endl<<"Reset(65)"<<endl;
	bitmap.Reset(65);
	cout<<"65?"<<bitmap.Test(65)<<endl;

	cout<<endl<<"Size:"<<bitmap.Size()<<endl;
}
#include <iostream>
using namespace std;
//#include "HashTablesBucket.h"
#include "BitMap.h"
int main()
{
	//TestDic();
	//Test();
	BitMapTest();

	return 0;
}

技术分享

本文出自 “zgw285763054” 博客,请务必保留此出处http://zgw285763054.blog.51cto.com/11591804/1786883

C++实现位图

原文:http://zgw285763054.blog.51cto.com/11591804/1786883

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!