手写queue

· · 个人记录

众所周知 STL 特别慢,于是想手写 STL

本节为 queue ,以下为代码:

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
template<class T>
class vector{
    public:
        T* p=NULL;//数组的首指针 
        int tot; //数组的长度
        int siz;//数组的容量
        vector(int b){//初始化一个长度为b的数组 
            this->siz=b;
            this->tot=0;
            p=(T*)malloc(b*sizeof(T));
        }
        vector(){//初始化一个长度为1的数组 
            this->siz=1;
            this->tot=0;
            p=(T*)malloc(sizeof(T));
        }
        T* reserve(int len){//将数组容量改为len 
            this->siz=len;
            p=(T*)realloc(p,len*sizeof(T));
            return p;
        }
        void swap(vector a,vector b){//交换两个数组,时间复杂度为O(1) 
            T* p1=a.p;
            a.p=b.p;
            b.p=p1;
            int tot1=a.tot;
            a.tot=b.tot;
            b.tot=tot1;
            int size1=a.siz;
            a.siz=b.siz;
            b.siz=size1;
        }
        T at(int i){//访问下标为i的元素,若越界则输出Error 
            if(i>=this->tot){
                cerr<<"Error"<<'\n';
                exit(0);
            }
            return *(p+i);
        }
        T operator [](int i){//访问下标为i的元素
            return *(p+i);
        }
        T operator =(vector<T> a){//赋值,时间复杂度为O(1) 
            this->p=(T*)realloc(a.p,a.siz*sizeof(T));
            this->siz=a.siz;
            this->tot=a.tot;
        } 
        int size(){//返回数组容量 
            return this->siz;
        }
        int length(){//返回数组长度 
            return this->tot;
        }
        bool empty(){//判断数组是否为空 
            return tot==0;
        }
        T front(){//返回数组的首个元素 
            if(empty()){
                cerr<<"Error"<<'\n';
                exit(0);
            } 
            return *p;
        }
        T back(){//返回数组最后的元素 
            if(empty()){
                cerr<<"Error"<<'\n';
                exit(0);
            }
            return *(p+tot-1);
        }
        T* data(){//返回数组的首指针 
            return p;
        }
        void push_back(T x){//往数组末尾添加一个元素 
            if(tot==siz){
                this->reserve(siz*2);
            }
            ++(this->tot);
            *(p+tot-1)=x;
        }
        void pop_back(){//将数组末尾的元素弹出 
            if(empty()){
                cerr<<"Error"<<'\n';
                exit(0);
            }
            --(this->tot);
        }
        void clear(){//清空数组 
            for(int* i=p;i!=p+tot-1;++i){
                (*i)=0;
            }
            tot=0;
        }
};
template<class T>
class queue{
    public:
        int head,fail;
        vector<T> q;
        queue(){//初始化队列 
            head=0;
            fail=0;
        }
        void push(T x){//往队尾加入一个元素 
            q.push_back(x);
            ++fail;
        }
        T front(){//访问队首
            if(head>fail){
                cerr<<"Error"<<'\n';
                exit(0);
            }
            return *(q.p+head);
        }
        int size(){//返回元素个数 
            return fail-head;
        }
        bool empty(){//返回是否为空 
            return head==fail;
        }
        void pop(){//在队首弹出一个元素 
            if(empty()){
                cerr<<"Error"<<'\n';
                exit(0);
            }
            ++head;
        } 
};
queue<int> q; 
int main(){ 
    q.push(114514);
    q.push(1919810);
    cout<<q.front()<<' '<<'\n';
    q.pop();
    cout<<q.front();
    return 0;
}
/*
114514
1919810
*/