Day6(C++)

文章展示了如何使用C++实现一个基本的动态数组类`myVector`,包括构造、拷贝构造、拷贝赋值、扩容、添加元素等操作。同时,还定义了一个循环队列类`cirQueue`,具备插入、删除、扩容等功能,并处理了队列为空的情况。
摘要由CSDN通过智能技术生成
c++完成vector函数功能
#include <iostream>

using namespace std;

template <typename T>
class myVecto
{
private:
    T *first;   //记录堆区空间的起始地址
    T *last;    //记录容器中最后一个元素的下标位置
    T *end;     //最大容量的最后一个元素的下标

public:
//        构造函数
    myVecto():first(nullptr), last(nullptr), end(nullptr) {}
    myVecto(int num, const T& val):first(new T[num])
    {
        for(int i=0; i<num; i++)
        {
            first[i] = val;
        }
        last = end = first+num;
    }
//        析构函数
    ~myVecto()
    {
        delete []first;
    }
//        拷贝构造
    myVecto(const myVecto &other):first(new T[other.end-other.first])
    {
        last = first+(other.last - other.first);
        end = first + (other.end - other.first);
        memcpy(first, other.first, (last-first)*sizeof (T));
    }
//        拷贝赋值
    myVecto& operator=(const myVecto& other)
    {
        delete [] first;
        first = new T[other.last - other.first];
        last = first + (other.last - other.first);
        end = first + (other.end - other.first);
        memcpy(first, other.first, (last-first)*sizeof (T));
        return *this;
    }
//        at()函数,返回下标对应的数字
    T& at(int sub)
    {
        if(sub<0 || sub>last-first)
        {
            cout << "段错误" << endl;
        }
        return first[sub];
    }
//        empty()
    bool empty()
    {
        return last == first ? true:false;
    }
//        full()
    bool full()
    {
        return last == end ? true:false;
    }
//        front()   返回第一个元素
    T& front()
    {
        return *first;
    }
//        back()    返回最后一个元素
    T& back()
    {
        return *(end-1);
    }
//        size()    返回Vector元素数量的大小
    int size()
    {
        return last-first;
    }

//        clear()   清空所有元素
    void clear()
    {
        last = first;
    }
//        expand()     二倍扩容函数
    void expand()
    {
        myVecto tmp;
        tmp.first = new T[(end-first) * 2];

        delete [] first;
    }
//        push_back()
    void push_back(const T& val)
    {
        if(full())
        {
            expand();
        }
        *last++ = val;
    }
//        pop_back()
    void pop_back()
    {
        if(empty())
        {
            cout << "空,不可在删除" << endl;
        }
        else
        {
            last--;
        }
    }

    void show()
    {
        for(auto i=this->first; i<this->last; i++)
        {
            cout << *i << " ";
        }
        cout << endl;
    }
};

int main()
{
    //有参构造
    myVecto<int> v1(10, 1);
    v1.show();

    //拷贝赋值
    myVecto<int> v2 = v1;
    v2.show();
    cout << "vector.capacity = " << v2.size() << endl;
    //进栈
    v2.push_back(3);
    v2.show();
    cout << "vector.capacity = " << v2.size() << endl;
    return 0;
}

输出结果
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
vector.capacity = 10
14427280 0 15734736 0 1 1 1 1 1 1 3
vector.capacity = 11
循环队列
#include <iostream>

using namespace std;

//自定义异常处理类
class ERROR
{
private:
    string msg;     //存储错误信息

public:
    ERROR() {}
    ERROR(string m):msg(m) {}

    //自定义what函数
    string what()
    {
        return msg;
    }
};

//模板函数
template <typename T>
class circuqueue
{
private:
    int* queue;      //存储数据
    int size;    //记录空间大小
    int front;      //头
    int rear;       //尾

public:
    //构造函数
    circuqueue() {};
    circuqueue(int s):size(s+1), front(0), rear(0)
    {
        queue = new T[size];
    }
    //析构函数
    ~circuqueue()
    {
        delete [] queue;
        queue = nullptr;
    }
    //拷贝构造(
    circuqueue(const circuqueue &other)
    {
        queue = new T[other.size];
        front = other.front;
        rear = other.rear;

        int i = other.front;
        for(; i!=other.rear; i=(i+1)%other.size)
        {
            queue[i] = other.queue[i];
        }
    }
    //拷贝赋值函数
    circuqueue & operator=(const circuqueue &other)
    {
        if(this == &other)
        {
            return *this;
        }
        delete [] queue;
        int i = other.front;
        for(; i!=other.rear; i=(i+1)%other.size)
        {
            queue[i] = other.queue[i];
        }

        front = other.front;
        rear = other.rear;
        size = other.size;

        return *this;
    }
    //判空
    bool empty()
    {
        return front == rear;
    }
    //判满
    bool full()
    {
        return front == (rear+1)%size;
    }
    //2扩容
    void resize()
    {
        int *temp = new int[size * 2];
        int j=0;

        for(int i=front; i!=rear; i=(i+1)%size)
        {
            temp[j++] = queue[i];
        }
        front = 0;
        rear = j;
        delete []queue;
        queue = temp;
        temp = nullptr;
        size *= 2;

    }

    //队尾插入
    void push(int val)
    {
        //如果插不进去,则进行扩容
        if(full())
        {
           resize();
        }

        queue[rear] = val;
        rear = (rear + 1)%size;
    }
    //出队
    void pop()
    {
        //判空
        if(empty())
        {
            throw ERROR("队列不能为空");
        }
        front = (front+1)%size;
    }
    //返回第一个元素
    T & front1()
    {
        return queue[front];
    }
    //返回队尾元素
    T & back()
    {
        return queue[(rear-1+size)%size];
    }



};

int main()
{
    cout << "ninf" << endl;
    circuqueue<int> q1;
    //循环入队
    cout << "cie" << endl;
    for(int i=0; i<9; i++)
    {
        q1.push(i);
        cout << q1.front1() << endl;
    }
    //头删
    q1.pop();
    for(int i=0; i<9; i++)
    {
        cout << q1.front1();
    }
    cout << endl;


    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值