C++ day7

没搞明白

main.cpp


#include <iostream>
#include <cstring>

using namespace std;

template<typename T>
class myVector {
private:
    T *first;
    T *last;
    T *end;
public:
    myVector();//无参函数
    myVector(int num, const T &val);//有参函数
    ~myVector();//析构函数
    myVector(const myVector &form);//拷贝函数
    T &at(int loc);//at()函数
    myVector &operator=(const myVector &from);//运算符重载,拷贝赋值


    //判空
    bool empty() {
        return first == last;
    }

    //判满
    bool full() {
        return last == end;
    }

    //返回第一个元素的函数
    T front() {
        return *first;
    }

    //返回最后一个元素的函数
    T back() {
        return *(last - 1);
    }

    //返回表中元素个数的函数
    int size() {
        return last - first;
    }

    //删除所有元素函数
    void clear() {
        while (last != first) {
            pop_back();
        }
    }

    //二倍扩充函数

    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 e) {
        //判满
        if (full() == true) {
            cout << "该链表已满,添加失败" << endl;
            cout << "准备二倍扩充" << endl;
            expand();
        }
        *last = e;
        last++;
        cout << "插入成功" << endl;
    }

    //删除最后一个元素函数
    void pop_back() {
        //判空
        if (empty() == true) {
            cout << "删除失败,该链表为空" << endl;
            return;
        }
        last--;
        cout << "删除成功" << endl;
    }


};

template<typename T>
myVector<T> &myVector<T>::operator=(const myVector &from) {
    int size_l = from.last - from.first + 1;
    int size_e = from.end - from.first;
    if (from.end == from.first && first == nullptr) {
        this->first = nullptr;
        this->last = nullptr;
        this->end = nullptr;
    } else if (from.end == from.first) {
        delete[]this->first;
        this->first = new T[size_l];
        this->end = first;
    } else {
        delete[]this->first;
        this->first = new T[size_l];
        memset(this->first, 0, sizeof(T) * size_l);
        memcpy(this->first, from.first, sizeof(from) * size_l);
        this->end = this->first + size_e;
        this->last = this->first + size_l - 1;
    }
    return *this;
}


//无参构造
template<typename T>
myVector<T>::myVector():first(nullptr), last(nullptr), end(nullptr) {
    cout << "无参构造" << endl;
}

//有参构造
template<typename T>
myVector<T>::myVector(int num, const T &val):first(nullptr), last(nullptr), end(nullptr) {
    if (num < 1) {
        throw -1;
    }
    first = new T[num];
    memset(first, 0, sizeof(T) * num);
    last = first + num - 1;
    T *temp = first;
    for (int i = 0; i < num; i++, temp++) {
        *temp = val;
    }
    end = temp;
    cout << "有参构造" << endl;
}

//析构函数
template<typename T>
myVector<T>::~myVector() {
    if (nullptr != first) {
        delete[]first;
        first = end = last = nullptr;
    }
    cout << "析构函数" << endl;
}

//拷贝构造
template<typename T>
myVector<T>::myVector(const myVector &from) {
    if (nullptr != from.first) {
        int other_size = from.last - from.first + 1;//容量
        first = new T[other_size];
        last = first + other_size - 1;
        end = first + (int) (from.end - from.first);
        //拷贝数据
        memcpy(first, from.first, sizeof(T) * other_size);
    } else {
        first = end = last = nullptr;
    }
    cout << "拷贝构造" << endl;
}

//at
template<typename T>
T &myVector<T>::at(int loc) {
    if (nullptr == first || nullptr == end) {
        throw -1;
    } else if (loc < 0 || loc >= end - first) {
        throw -1;
    } else {
        return *(first + loc);
    }
}

int main() {
    myVector<char> c1(3, 'c');
    myVector<char> c2(c1);
    myVector<char> c3;
    c3 = c1;

    c1.push_back('b');
    c1.push_back('c');
    c1.push_back('d');

    cout << c1.at(1) << endl;
    cout << c1.at(3) << endl;
    cout << c1.at(4) << endl;
    cout << "表中元素个数为:" << c1.size() << endl;
            cout << "第一个元素为:" << c1.front() << endl;
                                           cout << "最后一个元素为:" << c1.back() << endl;
                                              //调用在末尾删除函数
                                              c1.pop_back();
    cout << "表中元素个数为:" << c1.size() << endl;
                                              //清空
                                              c1.clear();
    cout << "表中元素个数为:" << c1.size() << endl;

        return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值