#include <iostream> using namespace std; #include <vector> class BitMap { public: BitMap() :_size(0) {} BitMap(size_t len) :_size(0) { size_t size = len/32+1; _array.resize(size, 0); } void Set(size_t num) { //size_t index = num/32; size_t index = num>>5; size_t count = num%32; _array[index] |= 1<<count; ++_size; } void ReSet(size_t num) { size_t index = num>>5; size_t count = num%32; _array[index] &= ~(1<<count); --_size; } bool Test(size_t num) { size_t index = num>>5; size_t count = num%32; return _array[index]&(1<<count); } void Clear() { _array.assign(_array.size(), 0); _size = 0; } private: vector<int> _array; size_t _size; }; void Test1() { BitMap bm(-1); bm.Set(100); bm.Set(9999); bm.Set(111111); cout<<"100?"<<bm.Test(100)<<endl; cout<<"99?"<<bm.Test(99)<<endl; cout<<"111111?"<<bm.Test(111111)<<endl; cout<<"101?"<<bm.Test(101)<<endl; cout<<"9999?"<<bm.Test(9999)<<endl; bm.ReSet(100); cout<<"100?"<<bm.Test(100)<<endl; } int main() { Test1(); return 0; }
时间: 2024-12-22 08:50:10