Live2D

vector类的简单实现

vector支持很多种数据类型,故要定义成模板类

 

0、数据成员

  • 长度 theSize
  • 容量 theCapacity
  • 指针 T* array
  • 另外还要指定容量的增长步长
1
2
3
4
int theSize;
int theCapacity;
T* array;
#define WALK_LENGTH 64;

  

1、构造函数

  • 无参数构造函数
  • 用几个相同值初始化的构造函数
  • 拷贝构造函数
  • 析构函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    myVector():theSize(0),theCapacity(0),array(NULL){}
myVector(const T& target , int num):theSize(0),theCapacity(0),array(NULL)
{
    while( num-- )
        push_back(target);
}
myVector(const myVector<T>& other):theSize(0),theCapacity(0),array(NULL)
{
    // 已重载本类=操作符,有开辟新空间,仍属于深拷贝
    *this = other; //重载=
}
~myVector()
{
    clear();
}   

  

2、基本成员函数

  • 长度获取
  • 容量获取
  • 是否为空
  • 清空
  • 打印
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
    int size() const
{
    return theSize;
}
 
int capacity() const
{
    return theCapacity;
}
 
bool empty()
{
    return theSize==0;
}
 
void clear()
{
    if( array )
        delete array;
    array = NULL;
    theSize = 0;
    theCapacity = 0;
}
 
void printArray()
{
    for( int i=0; i<theSize; i++ )
        cout<<array[i]<<" ";
    cout<<",size="<<theSize<<" ,capacity="<<theCapacity<<endl;
}

  

3、实现增删

  • 在头插入
  • 在尾插入
  • 在指定位置插入
  • 删除指定位置
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
void push_back(const T& target)
{
    insert_before(theSize, target);
}
 
void push_front(const T& target)
{
    insert_before(0, target);
}
 
void insert_before(const int& pos, const T& target)
{
    if(theSize == theCapacity)
    {
        /* array没有delete之前,原来的空间仍然存在,
        /* 当array申请了新空间,只是失去了旧空间的指向,
        /* 用oldarray指向旧空间,等新空间拷贝完,再delete旧空间
        */
        T* oldarray = array;
        theCapacity += WALK_LENGTH;
        array = new T[theCapacity];
        for( int i=0; i<theSize; i++ )
            array[i] = oldarray[i];
        delete oldarray;
    }
 
    for( int i=theSize; i>pos; i-- )
        array[i] = array[i-1];
    array[pos] = target;
    theSize++;
}
 
void erase(const int& pos)
{
    if( pos < theSize )
    {
        for( int i=pos; i<theSize; i++ )
            array[i] = array[i+1];
        theSize--;
    }
}

  

4、操作符重载

  • 赋值操作符 = 
  • 下标操作符  [ ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
myVector<T>& operator = (const myVector<T>& other)
{
    //参数other为const 所以other调用的函数都应定义为const
    if( this == &other )
        return *this;
    clear();
    theSize = other.size();
    theCapacity = other.capacity();
    array = new T[theCapacity];
    for( int i=0; i<theSize; i++ )
        array[i] = other[i]; //重载[]
    return *this;
}
 
T& operator [] ( const int& pos ) const
{
    assert(pos<theSize);
    return array[pos];
}

  

 

总结:整体代码

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#include <assert.h>
using namespace std;
 
template<typename T>
class myVector
{
private:
    int theSize;
    int theCapacity;
    T* array;
    #define WALK_LENGTH 64;
public:
    myVector():theSize(0),theCapacity(0),array(NULL){}
    myVector(const T& target , int num):theSize(0),theCapacity(0),array(NULL)
    {
        while( num-- )
            push_back(target);
    }
    myVector(const myVector<T>& other):theSize(0),theCapacity(0),array(NULL)
    {
        // 已重载本类=操作符,有开辟新空间,仍属于深拷贝
        *this = other; //重载=
    }
    ~myVector()
    {
        clear();
    }
 
    myVector<T>& operator = (const myVector<T>& other)
    {
        //参数other为const 所以other调用的函数都应定义为const
        if( this == &other )
            return *this;
        clear();
        theSize = other.size();
        theCapacity = other.capacity();
        array = new T[theCapacity];
        for( int i=0; i<theSize; i++ )
            array[i] = other[i]; //重载[]
        return *this;
    }
 
    T& operator [] ( const int& pos ) const
    {
        assert(pos<theSize);
        return array[pos];
    }
 
    void clear()
    {
        if( array )
            delete array;
        array = NULL;
        theSize = 0;
        theCapacity = 0;
    }
 
    int size() const
    {
        return theSize;
    }
 
    int capacity() const
    {
        return theCapacity;
    }
 
    bool empty()
    {
        return theSize==0;
    }
 
    void push_back(const T& target)
    {
        insert_before(theSize, target);
    }
 
    void push_front(const T& target)
    {
        insert_before(0, target);
    }
 
    void insert_before(const int& pos, const T& target)
    {
        if(theSize == theCapacity)
        {
            /* array没有delete之前,原来的空间仍然存在,
            /* 当array申请了新空间,只是失去了旧空间的指向,
            /* 用oldarray指向旧空间,等新空间拷贝完,再delete旧空间
            */
            T* oldarray = array;
            theCapacity += WALK_LENGTH;
            array = new T[theCapacity];
            for( int i=0; i<theSize; i++ )
                array[i] = oldarray[i];
            delete oldarray;
        }
 
        for( int i=theSize; i>pos; i-- )
            array[i] = array[i-1];
        array[pos] = target;
        theSize++;
    }
 
    void erase(const int& pos)
    {
        if( pos < theSize )
        {
            for( int i=pos; i<theSize; i++ )
                array[i] = array[i+1];
            theSize--;
        }
    }
 
    void printArray()
    {
        for( int i=0; i<theSize; i++ )
            cout<<array[i]<<" ";
        cout<<",size="<<theSize<<" ,capacity="<<theCapacity<<endl;
    }
};
 
int main()
{
    myVector<int> vec1; //无参数构造函数
 
    cout<<"相同值赋值的构造函数"<<endl;
    myVector<int> vec2(2,4);
    vec2.printArray();
 
    cout<<"拷贝构造函数"<<endl;
    myVector<int> vec3(vec2);
    vec3.printArray();
 
    cout<<"在头插入 1"<<endl;
    vec2.push_front(1);
    vec2.printArray();
 
    cout<<"删除位置11"<<endl;
    vec2.erase(11);
    vec2.printArray();
 
    cout<<"删除位置0"<<endl;
    vec2.erase(0);
    vec2.printArray();
 
    cout<<"在尾插入3"<<endl;
    vec2.push_back(3);
    vec2.printArray();
 
    cout<<"在位置2之前插入4"<<endl;
    vec2.insert_before(2,4);
    vec2.printArray();
 
    cout<<"操作符=重载"<<endl;
    vec1 = vec2;
    vec1.printArray();
 
    return 0;
}

  执行结果:

 

posted @   Christal_R  阅读(245)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示