C++STL自己实现

容器自己实现

Posted by WenlSun" on May 2, 2020

实现自己的vector容器

参考代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>

using namespace std;

/**
 * vector 底层实现
 */

template <typename T>

class MyVector {
   public:
    // 无参构造初始化
    MyVector() : p(nullptr), cap(0), sz(0) {}
    // 有参构造的初始化方式
    MyVector(int size, T data) {
        this->cap = 20 + size;
        this->sz = size;
        this->p = new T[this->cap];
        for (int i = 0; i < this->sz; i++) {
            this->p[i] = data;
        }
    }

    // 析构函数,释放掉唯一的指针
    ~MyVector() {
        if (p != nullptr) {
            delete[] p;
        }
    }

    // 拷贝构造函数
    MyVector(const MyVector& v) {
        this->cap = v.capacity();
        this->sz = v.size();
        this->p = new T[this->cap];
        memcpy(this->p, v.p, this->sz * sizeof(T));
    }

    // 插入,要判断溢出没
    void push_back(T data) {
        if (this->p == nullptr) {
            this->cap = 20;
            this->sz = 0;
            this->p = new T[this->cap];
        }
        if (this->sz == this->cap) {
            T* new_p = new T[this->cap * 2];
            memcpy(new_p, this->p, this->sz * sizeof(T));
            this->cap *= 2;
            delete[] p;
            this->p = new_p;
        }
        this->p[this->sz] = data;
        this->sz++;
    }

    // 删除最后一个元素
    void pop_back() {
        if (this->sz > 1) {
            this->p[this->sz - 1] = 0;
            this->sz--;
        }
    }

    // 插入
    void insert(int pos, int data) {
        if (pos >= 0 && pos <= this->sz) {
            if (this->sz == this->cap) {
                T* new_p = new T[this->cap * 2];
                memcpy(new_p, this->p, this->sz * sizeof(T));
                this->cap * 2;
                delete[] p;
                this->p = new_p;
            }
            for (int i = this->sz; i > pos; i--) {
                this->p[i] = this->p[i - 1];
            }
            this->p[pos] = data;
            this->sz++;
        }
    }

    //清除,假装清除了
    void clear() { this->sz == 0; }

    // 重载[]运算符,可以用[]修改函数
    T& operator[](int index) {
        if (index >= 0 && index < this->sz) {
            return this->p[index];
        }
    }

    // 重载=运算符,其实和拷贝构造函数一样
    void operator=(const MyVector& v) {
        if (p != nullptr) {
            delete[] p;
            this->cap = 0;
            this->sz = 0;
            this->p = nullptr;
        }
        this->cap = v.capacity();
        this->sz = v.size();
        this->p = new T[this->cap];
        memcpy(this->p, v.p, this->sz * sizeof(T));
    }

    int size() { return this->sz; }

    int capacity() { return this->cap; }

   private:
    T* p;
    int cap;
    int sz;
};

int main() {
    MyVector<int> mv(3, 0);
    mv.push_back(34);
    mv.push_back(22);
    mv.push_back(10);
    mv.pop_back();
    mv.insert(2, 100);
    for (int i = 0; i < mv.size(); i++) {
        cout << mv[i] << endl;
    }

    return 0;
}