C++STL手写版

手写STL,卡常专用。

node为变量类型,可以自由定义,以下不再赘述。

1、stack(栈)

  开一个数组,和一个top指针,压栈时++,弹栈时--即可。

struct stack{
    int tp;node st[N];
    node top(){return st[tp];}
    void push(node x){st[++tp]=x;}
    void pop(){--tp;}
    bool empty(){return !tp;}
    void size(){return tp;}
    void clear(){tp=0;}
}s;

2、queue(循环队列)

  用两个指针,记录队头和队尾,达到两端时特判即可。

struct queue{
    int l,r,cnt;node qu[N];
    node front(){return qu[l];}
    void ne(int &x){++x;if(x==N) x=1;}
    void push(node x){ne(r);qu[r]=x;++cnt;}
    void pop(){ne(l);--cnt;}
    bool empty(){return !cnt;}
    void size(){return cnt;}
    void clear(){r=cnt=0,l=1;}
}q;

3、deque(双端队列)

  和队列类似。

struct deque{
    int l,r,cnt;node qu[N];
    void init(){r=cnt=0,l=1;}
    node front(){return qu[l];}
    node back(){return qu[r];}
    void ne(int &x){++x;if(x==N) x=1;}
    void la(int &x){--x;if(!x) x=N-1;}
    void push_front(node x){la(l);qu[l]=x;++cnt;}
    void push_back(node x){ne(r);qu[r]=x;++cnt;}
    void pop_front(){ne(l);--cnt;}
    void pop_back(){la(r);--cnt;}
    bool empty(){return !cnt;}
    void size(){return cnt;}
    void clear(){r=cnt=0,l=1;}
}q;
扫描二维码关注公众号,回复: 7441209 查看本文章

4、priority_queue(优先队列)

  具有强大的功能,可以删除任意一个元素。

  使用时在node内部重载"<"号,删除元素传该元素的id。

struct priority_queue{
    int cnt,p[N];node heap[N];
    void up(int x){
        while(x>1&&heap[x>>1]<heap[x]){
            swap(p[heap[x>>1].id],p[heap[x].id]);
            swap(heap[x>>1],heap[x]);
            x>>=1;
        }
    }
    void down(int x){
        int y=x<<1;
        while(y<=cnt){
            if(y<cnt&&heap[y]<heap[y|1]) y|=1;
            if(heap[x]<heap[y]){
                swap(p[heap[x].id],p[heap[y].id]);
                swap(heap[x],heap[y]);
                x=y;y=x<<1;
            }
            else break;
        }
    }
    int size(){return cnt;}
    bool empty(){return !cnt;}
    void push(node x){heap[++cnt]=x;p[x.id]=cnt;up(cnt);}
    node top(){return heap[1];}
    void pop(){
        swap(p[heap[1].id],p[heap[cnt].id]);
        swap(heap[1],heap[cnt]);
        p[heap[cnt].id]=0;cnt--;
        if(cnt) down(1);
    }
    void remove(int x){
        x=p[x];
        swap(p[heap[x].id],p[heap[cnt].id]);
        swap(heap[x],heap[cnt]);
        p[heap[cnt].id]=0;cnt--;
        if(x<=cnt){up(x);down(x);}
    }
    void clear(){cnt=0;}
}q;

5、bitset(多位二进制数)

  ct数组需预处理,表示每个数中1的个数。

struct bitset{
    unsigned long long a[N];
    const unsigned long long m=65535;
    void clear(){memset(a,0,sizeof(a));}
    void set(){for(int i=0;i<N;i++) a[i]=m+(m<<16)+(m<<32)+(m<<48);}
    void reset(){clean();}
    void flip(){for(int i=0;i<N;i++) a[i]^=(m+(m<<16)+(m<<32)+(m<<48));}
    void set(int x,int y){
        if(y==1) a[x/64]|=(unsigned long long)1<<(x&63);
        else if((a[x/64]>>(x&63))&1) a[x/64]^=(unsigned long long)1<<(x&63);
    }    
    void reset(int x){set(x,0);}
    void flip(int x){
        a[x/64]^=(unsigned long long)1<<(x&63);
    }
    friend bitset operator | (const bitset &x,const bitset &y){
        bitset ans;ans.clear();
        for(int i=0;i<N;i++) ans.a[i]=x.a[i]^y.a[i];
        return ans;
    }
    friend bitset operator & (const bitset &x,const bitset &y){
        bitset ans;ans.clear();
        for(int i=0;i<N;i++) ans.a[i]=x.a[i]^y.a[i];
        return ans;
    }
    friend bitset operator ^ (const bitset &x,const bitset &y){
        bitset ans;ans.clear();
        for(int i=0;i<N;i++) ans.a[i]=x.a[i]^y.a[i];
        return ans;
    }
    friend void operator |= (bitset &x,const bitset &y){
        for(int i=0;i<N;i++) x.a[i]|=y.a[i];
        return ;
    }
    friend void operator &= (bitset &x,const bitset &y){
        for(int i=0;i<N;i++) x.a[i]&=y.a[i];
        return ;
    }
    friend void operator ^= (bitset &x,const bitset &y){
        for(int i=0;i<N;i++) x.a[i]^=y.a[i];
        return ;
    }
    int count(){
        int cnt=0;
        for(int i=0;i<N;i++) cnt+=ct[a[i]&m]+ct[(a[i]>>16)&m]+ct[(a[i]>>32)&m]+ct[(a[i]>>48)&m];
        return cnt;
    }
    bool any(){return count();}
    bool none(){return !count();}
}b;

6、set(红黑树)

  此处暂留坑......(我太菜了)

猜你喜欢

转载自www.cnblogs.com/hz-Rockstar/p/11643946.html