zoukankan      html  css  js  c++  java
  • BitMap

    c++实现的BitMap

    参考:https://blog.csdn.net/kl1106/article/details/79478787

    #include<bits/stdc++.h>
    using namespace std;
    /**
        BitMap
        用于判断上百亿数中,哪个数重复出现
        用每一位做标志位
    */
    class BitMap{
    private:
        //空间首指针
        char* s;
        //能存储多少个数
        int number;
    
    public:
        //有tempNumber个数
        BitMap(int tempNumber){
            number = tempNumber;
            s = (char *)malloc(sizeof(char) * ((number>>3) + 1));
        }
        void add(int n){
            int index = n >> 3;
            int position = n & 0x07;
            //1<<position 就是对应位为1 或一下将该位设为1,表示添加进来
            s[index] |= (1<<position);
        }
        void clear(int n){
            int index = n >> 3;
            int position = n & 0x07;
            //1 <<position 在 ~ 一下,对应位为0 其他位为1,与一下将这位置为0 表示清除掉
            s[index] &= ~(1<<position);
        }
        bool contains(int n){
            int index = n >> 3;
            int position = n & 0x07;
            //找到对应位,判断是不是0
            //注意:如果是和1比较,会出错
            return ((s[index] & (1 << position)) != 0);
        }
    };
    int main(){
        BitMap tm(10);
        tm.add(2);
        cout << tm.contains(2) << endl;
        tm.clear(2);
        cout << tm.contains(2) << endl;
    
        return 0;
    }
  • 相关阅读:
    HDU 5795
    HDU5783
    HDU 5791
    VK Cup 2016
    Codeforces Round #357 (Div. 2)
    Educational Codeforces Round 15
    HDU5724
    博弈学习 3
    Spring的多配置文件加载
    spring 核心技术
  • 原文地址:https://www.cnblogs.com/gudygudy/p/11102938.html
Copyright © 2011-2022 走看看