BitMap实现

#pragma once

善左ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!

#include

using namespace std;

class BitMap

{

public:

BitMap()

:_size(0)

{}

BitMap(size_t size)

:_size(0)

{

_arrays.resize((size >> 5) + 1);//BitMap的大小

}

bool Set(size_t num)//在对应的index下的(1<

{

size_t index = num >> 5;

size_t n = num % 32;

if (_arrays[index] & (1 << n))

{

return false;

}

else

{

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

++_size;

return true;

}

}

bool ReSet(size_t num)//去掉标志位

{

size_t index = num >> 5;

size_t n = num % 32;

if (_arrays[index] & (1 << n))

{

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

--_size;

return true;

}

return false;

}

bool Test(size_t num)

{

size_t index = num >> 5;

size_t n = num % 32;

return _arrays[index] & (1 << n);

}

void Clear()

{

_arrays.assign(_arrays.size(), 0);

}

protected:

vector _arrays;

size_t _size;

};

void Test1()

{

BitMap bm(65);

bm.Set(1);

bm.Set(4);

bm.Set(33);

cout << "1?" << bm.Test(1) << endl;

cout << "2?" << bm.Test(2) << endl;

cout << "4?" << bm.Test(4) << endl;

cout << "33?" << bm.Test(33) << endl;

bm.ReSet(33);

bm.ReSet(4);

cout << "1?" << bm.Test(1) << endl;

cout << "2?" << bm.Test(2) << endl;

cout << "4?" << bm.Test(4) << endl;

cout << "33?" << bm.Test(33) << endl;

}


当前题目:BitMap实现
转载来源:http://lszwz.com/article/jehooj.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款