C++day5作业

#include <iostream>
#include <cstring>
 
using namespace std;
 
template <typename T>
class Myvector
{
private:
    T* first; // 头指针
    T* end; // 尾元素的下一位置
    T* last; // 最大存储容量地址
 
public:
    // 构造函数
    Myvector() : first(new T[8]) {
        end = first; // 初始化尾指针
        last = first + 8; // 初始化最大存储容量地址
    }
 
    // 析构函数
    ~Myvector(){
        delete []first;
    }
 
 
    // 拷贝构造函数
    Myvector(const Myvector &other){
        // 申请空间
        this->first = new T[other.last - other.first];
        // 拷贝内容
        memcpy (this->first, other.first, sizeof(T) * (other.end - other.first));
        // 设置尾指针
        this->end = this->first + (other.end - other.first);
        // 设置最大存储容量地址
        this->last = this->first + (other.last - other.first);
    }
 
 
    // 拷贝赋值
    Myvector&operator=(const Myvector &other){
        if (this != &other) {
            // 释放原有空间
            delete []this->first;
            // 申请空间
            this->first = new T[other.last - other.first];
            // 拷贝内容
            memcpy (this->first, other.first, sizeof(T) * (other.end - other.first));
            // 设置尾指针
            this->end = this->first + (other.end - other.first);
            // 设置最大存储容量地址
            this->last = this->first + (other.last - other.first);
        }
        return *this;
    }
 
 
    // at
    T &at(int index){
        try {
            // 检查下标是否合法
            if (index < 0 || index > end - first) {
                // 抛出异常
                throw string("参数不合法");
            }
        } catch (string err) {
            cout << err << endl; // 打印异常
        }
        // 返回对应位置引用
        return *(first + index);
    }
 
 
    // 判空函数
    bool empty(){
        return this->first == this->end ? true : false;
    }
 
 
    // 判满函数
    bool full(){
        return this->end - 1 == this->last ? true : false;
    }
 
 
    // 获取第一个元素
    T & front(){
        try {
            // 判空
            if (empty ()){
                // 抛出异常
                throw string("没有元素");
            }
 
            // 返回第一个元素引用
            return *(this->first);
        } catch (string err) {
            cout << err << endl; // 打印异常
        }
    }
 
 
    // 获取最后一个元素
    T & back(){
        try {
            // 判空
            if (empty ()){
                // 抛出异常
                throw string("没有元素");
            }
 
            // 返回第一个元素引用
            return *(this->end - 1);
        } catch (string err) {
            cout << err << endl; // 打印异常
        }
    }
 
    // 获取当前大小
    size_t size(){
        return sizeof(T) * (this->end - 1 - this->first);
    }
 
    // 清空
    void claer(){
        this->end = this->first;
    }
 
 
    // 二倍扩容
    void expand(){
        int size = this->last - this->first;
        size_t n = sizeof(T) * size;
        // 申请二倍空间
        T* temp = new T[size * 2];
        // 拷贝内容
        memcpy (temp, this->first, n);
        // 释放原有空间
        delete []this->first;
        // 重新设置指针指向
        this->first = temp;
        this->end = this->first + n / sizeof(T);
        this->last = this->first + size * 2;
    }
 
    // 尾部插入
    void push_back(const T &val){
        // 判满
        if (full ()) {
            // 二倍扩容
            this->expand ();
        }
        // 插入
        *this->end = val;
        // 向后移动
        this->end = this->end + 1;
    }
 
 
    // 尾部删除
    void pop_back(){
        try {
            // 判空
            if (empty ()){
                // 抛出异常
                throw string("没有元素");
            }
 
            // 向前移动
            this->end = this->end - 1;
        } catch (string err) {
            cout << err << endl; // 打印异常
        }
    }
 
    // show
    void show(){
        for (int i = 0; i < end - first; ++i) {
            cout << at (i) << "\t";
        }
        cout << endl;
    }
};
 
 
int main()
{
    Myvector<int> v1;
    for (int i = 1; i < 18; ++i) {
        v1.push_back (i);
    }
 
 
    v1.show ();
 
 
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值