# 2023/4/3 vector和循环队列

2023/4/3 vector和循环队列

#include <iostream>

using namespace std;

template <typename T>
class Myvector
{
private:
    T * first;
    T * last;
    T * end;

public:
    //构造
    Myvector(int size = 2)
    {
        first = new T[size];
        first = last;
        end = first + last;
    }
    //析构函数
    ~Myvector()
    {
        delete [] first;
        first = last = end = nullptr;
    }
    //拷贝构造
    Myvector(const Myvector<T> & other)
    {
        int len = other.last - other.first;
        int size = other.end - other.first;

        this->first = new T[size];
        memcpy(this->first,other.first,len*sizeof(T));

        this->last = this->first + len;
        this->end = this->first + size;

    }
    //拷贝赋值
    Myvector<T> &operator=(const Myvector<T>&other)
    {
        int len = other.last - other.first;
        int size = other.end - other.first;

        this->first = new T[size];
        memcpy(this->first,other.first,len*sizeof(T));

        this->last = this->first + len;
        this->end = this->first + size;

        return *this;
    }


    //at()
    T &at(int pos)
    {
        if(pos < 0 || pos > this->len())
        {
            cout << "越界" << endl;
        }
        return *(first+pos);
    }
    //empty()
    bool empty()
    {
        return last == first ? true:false;
    }

    //full()
    bool full()
    {
        return end == last ? true:false;
    }

    //front()
   const T& front()const
    {
        return *first;
    }

    //back()
    const T& back()const
    {
        return *(last-1);
    }

    //size()
    int size()const
    {
        return end - first;
    }

    int len()const
    {
        return last - first;
    }

    //clear()
    void clear()
    {
        last = first;
    }

    //expand()  二倍扩容函数
    void expand()
    {
        int size = this->end - this->first;

        T *temp = new T[2*size];
        memcpy(temp->first,first,size*sizeof(T));
        delete []this;
        first = temp;
        last = first + size;
        end = first + 2*size;

    }

    //push_back()
    void push_back(const T&other)
    {
        if(this->full())
        {
            this->expand();
        }

        *last = other;
        last ++;

    }

    //pop_back()
    void pop_back()
    {
        if(this->empty())
        {
            return;
        }
        --last;
    }

};


template<typename T>
class MyQueue
{
private:
    T* queue;
    size_t maxSize;
    size_t front;
    size_t rear;
public:
    MyQueue(size_t s):queue(new T[s]),maxSize(s),front(0),rear(0){}
    ~MyQueue()
    {
        delete [] queue;
    }
    bool empty()const
    {
        return front==rear;
    }
    bool full()const
    {
        return (rear+1)%maxSize==front;
    }
    void dequeue()
    {
        if(empty())
        {
            throw ;
        }
        front=(front+1)%maxSize;
    }
    void enqueue(const T& val)
    {
        if(full())
        {
            throw ;
        }
        queue[rear]=val;
        rear=(rear+1)%maxSize;
    }
    int size() const
    {
        return (rear+maxSize-front)%maxSize;
    }
    void show()const
    {
        for(int i=0;i<size();i++)
        {
            cout<<queue[i]<<" ";
        }
        cout<<endl;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值