标签:include 源代码 public count 位图 bitmap
#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; }
本文出自 “剩蛋君” 博客,转载请与作者联系!
标签:include 源代码 public count 位图 bitmap
原文地址:http://memory73.blog.51cto.com/10530560/1842994