zoukankan      html  css  js  c++  java
  • C++简单版BitSet求解大量数据是否存在莫个数

    #include <iostream>
    using namespace std;
    template<int N>
    class BitSet
    {
    public:
        BitSet()
        {
            set();
        }
        void set()
        {
            for (int i = 0; i < Nm; i++)
            {
                vtr[i] = 0;
            }
        }
        void set(int n)
        {
            vtr[(n-1) / 32] |= (0x1 << ((n-1) % 32));
        }
        void Printf()
        {
            for (int i = N; i >0; i--)
            {
                cout<< (((vtr[(i-1)/32]>>((i-1)%32)) & 0x1)  == 0 ? "0" : "1");
            }
        }
        bool test(int n)
        {
            if (vtr[(n-1) / 32] >> ((n-1) % 32) & 0x1)return true;
            return false;
    
        }
    private:
        enum{ _N_=(N-1) / (sizeof(size_t)*8)};
        enum{Nm=_N_+1};
        int vtr[Nm];
    };
    int main()
    {
        BitSet<344> bt;
        bt.set(32);
        //如此一个简单的bitset就完毕了,找出大量数据中是否存在莫个数字。
        if (bt.test(32))
        {
            cout << "该位置存在" << endl;
        }
        else
        {
            cout << "该位置不存在" << endl;
        }
        bt.Printf();
        return 0;
    }
  • 相关阅读:
    ceph部署与问题
    openstack常见问题汇总
    css
    html
    zookeeper
    ZAB协议
    快速排序
    Servlet梳理
    tomcat性能优化梳理
    tomcat梳理
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/7103326.html
Copyright © 2011-2022 走看看