> 5) + 1);..."/>
溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

BitMap實現(xiàn)

發(fā)布時間:2020-07-16 02:21:15 來源:網(wǎng)絡(luò) 閱讀:307 作者: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)//在對應(yīng)的index下的(1<<n)位設(shè)置為一

{

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)//去掉標(biāo)志位

{

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問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI