位图
    image.png

    1. #include<iostream>
    2. #include<vector>
    3. using namespace std;
    4. class BitMap{
    5. public:
    6. BitMap(size_t range){
    7. _bits.resize(range/32+1);
    8. }
    9. void set(size_t x){
    10. int index=x>>5;
    11. int position=x&0x31;
    12. _bits[index]|=(1<<position);
    13. }
    14. void reset(size_t x){
    15. int index=x>>5;
    16. int position=x&0x31;
    17. _bits[index]&=~(1<<position);
    18. }
    19. bool isExist(size_t x){
    20. int index=x>>5;
    21. int position=x&0x31;
    22. return (_bits[index]&(1<<position))!=0;
    23. }
    24. private:
    25. vector<int> _bits;
    26. };
    27. void test(){
    28. BitMap bm(200);
    29. bm.set(136);
    30. bm.set(1);
    31. cout<<bm.isExist(136)<<endl;
    32. bm.reset(136);
    33. cout<<bm.isExist(136)<<endl;
    34. }
    35. int main(){
    36. test();
    37. system("pause");
    38. return 0;
    39. }