C++——仿照vector,实现MyVector

重写vector,函数包含:
构造函数
析构函数
拷贝构造
拷贝赋值
at()
empty()
full()
front()
back()
size()
clear()
expand() 二倍扩容函数
push_back()
pop_back()

代码如下:

 

/**
  ******************************************************************************
  * @file              : MyVector.cpp
  * @author            : XHJ
  * @date              : 2023/5/15
  * @description       : 重写vector,函数包含:
                        构造函数
                        析构函数
                        拷贝构造
                        拷贝赋值
                        at()
                        empty()
                        full()
                        front()
                        back()
                        size()
                        clear()
                        expand()     二倍扩容函数
                        push_back()
                        pop_back()
  ******************************************************************************
  */

#include <iostream>
#include <vector>

using namespace std;

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

    //定义无参构造
    MyVector() {
        first = new T[2];
        last = first;
        end = first + 1;
    }

    //定义有参构造
    MyVector(int num, const T &val) {
        first = new T[num + 1];
        last = first;
        end = first + num;
        for (int i = 0; i < num; ++i) {
            first[i] = val;
            last++;
        }
    }

    //定义拷贝构造函数
    MyVector(const MyVector<T> *other) {
        this->first = new T[other->end - other->first + 1];
        this->last = other->last;
        this->end = other->end;
        for (int i = 0; i < other->end - other->first; ++i) {
            this->first[i] = other->first[i];
        }
    }

    //定义拷贝赋值函数
    MyVector &operator=(const MyVector<T> *other) {
        if (this != other)  //防止自己给自己赋值
        {
            delete[]first;
            this->first = new T[other->end - other->first + 1];
            this->last = other->last;
            this->end = other->end;
            for (int i = 0; i < other->end - other->first; ++i) {
                this->first[i] = other->first[i];
            }
        }
        return *this;
    }

    //析构函数
    ~MyVector() {
        //手动释放堆区空间
        delete[] first;
        first = nullptr;
        last = nullptr;
        end = nullptr;
    }

    //at()函数
    T &at(int pos) {
        if (pos > end - first) {
            throw -1;           //越界访问
        }
        return first[pos];
    }

    //成员函数实现[]符号重载
    const T &operator[](const int pos) const {
        return first[pos];
    }

    //判空函数
    bool empty() {
        if (last == first)
            return true;
        else
            return false;
    }

    //判满函数
    bool full() {
        if (last == end)
            return true;
        else
            return false;
    }

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

    //back()函数
    T &back() {
        return *(end - 1);
    }

    //size()函数
    const int size() const {
        return last - first;
    }

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

    //expand()  二倍扩容
    void expand() {
        T *temp = new T[(end - first) * 2];
        for (int i = 0; i < end - first; ++i) {
            temp[i] = first[i];
        }
        delete[]first;
        first = temp;
        end = first + 2 * (end - first);
        temp = nullptr;
    }

    //push_back()
    void push_back(const T &val) {
        if (full())
            expand();
        *last = val;
        last++;
    }

    //pop_back()
    void pop_back() {
        if (empty())
            throw -2;   //容器已空
        last--;
    }

    //begin()
    const T *begin() const {
        return first;
    }

    //end()     返回一个指向当前vector末尾元素的下一位置的迭代器
    const T *pend() const {
        return last;
    }
};

int main() {
    //定义有参构造
    MyVector<int> v1(5, 6);
    for (int i = 0; i < v1.size(); ++i) {
        cout << "v1[" << i << "] = " << v1[i] << endl;
    }
    cout << "************************" << endl;

    //拷贝构造v2
    MyVector<int> v2(v1);
    for (int i = 0; i < v2.size(); ++i) {
        cout << "v2[" << i << "] = " << v2[i] << endl;
    }
    cout << "************************" << endl;

    //赋值构造函数
    MyVector<int> v3;
    v3 = v1;
    for (int i = 0; i < v3.size(); ++i) {
        cout << "v3[" << i << "] = " << v3[i] << endl;
    }
    cout << "************************" << endl;

    //清空v3
    v3.clear();
    cout << "v3.size() = " << v3.size() << endl;
    cout << "************************" << endl;

    //判空
    if (v3.empty())
        cout << "v3容器已空!" << endl;
    else
        cout << "v3容器非空!" << endl;
    cout << "************************" << endl;

    //v3尾插
    v3.push_back(1);
    v3.push_back(2);
    v3.push_back(3);
    v3.push_back(4);
    v3.push_back(5);
    for (int i = 0; i < v3.size(); ++i) {
        cout << "v3[" << i << "] = " << v3[i] << endl;
    }
    cout << "************************" << endl;

    //第一个元素
    cout << "v3.front = " << v3.front() << endl;
    //最后一个元素
    cout << "v3.back = " << v3.back() << endl;
    cout << "************************" << endl;

    //尾删
    try {
        v3.pop_back();
        v3.pop_back();
    } catch (int e) {
        if (e == -2)
            cout << "容器已空,尾删失败!" << endl;
    }
    for (int i = 0; i < v3.size(); ++i) {
        cout << "v3[" << i << "] = " << v3[i] << endl;
    }
    cout << "************************" << endl;

    //at()函数
    try {
        cout << "v3.at(0) = " << v3.at(0) << endl;
        cout << "v3.at(1) = " << v3.at(1) << endl;
        cout << "v3.at(10) = " << v3.at(10) << endl;
    } catch (int e) {
        if (e == -1)
            cout << "越界访问!" << endl;
    }
    return 0;
}

效果如下:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值