温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

BitMap实现

发布时间:2020-07-16 02:21:15 来源:网络 阅读:292 作者:zheng_feng 栏目:编程语言

#pragma once


#include<vector>

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<<n)位设置为一

{

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<size_t> _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;

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI