#include#include #include #include using namespace std; class BitMap { public: BitMap(int range) : segment(0), offset(0) { range = range / 32; if (range == 0) { range = 1; } bitTable.resize(range); } void setBit(int x) { calSegmentAndOffset(x); bitTable[segment] |= (1 << offset); } void removeBit(int x) { calSegmentAndOffset(x); bitTable[segment] ^= (1 << offset); } bool find(int x) { calSegmentAndOffset(x); int temp = bitTable[segment]; if (temp &= (1 << offset)) { return true; } return false; } void print() { for (int i = 0; i < bitTable.size(); i++) { bitset<32> a(bitTable[i]); cout << a << " "; } cout << endl; } private: void calSegmentAndOffset(int x) { segment = x / 32; offset = x % 32; } int segment; int offset; vector bitTable; }; int main() { BitMap bm(64); bm.setBit(10); bm.setBit(33); cout << bm.find(9) << endl; cout << bm.find(10) << endl; bm.print(); bm.removeBit(10); bm.print(); return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)