Kyle Chen's Blog

Action speaks louder than Words

0%

用C++实现简易版vector

https://github.com/KyleAndKelly/miniSTL

实现一个简易版的Vector

  • miniVector.h
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
#include<iostream>
#include<string.h>
#include"assert.h"
using namespace std;


template<typename T>
class miniVector{
private:

T* vectorArr;
int capacity ;
int size ;
void memoryExpand(const int elem);
public:
~miniVector();
miniVector():vectorArr(NULL),capacity(2),size(0) {};
miniVector(const int cap);
miniVector(const int ini_size,T ini_data);
miniVector(const miniVector& vec);
miniVector& operator=(const miniVector& vec);

void push_back(const T& num);
void print_info() const;
T& operator[](const int& index) const;
void insert(const int& index,const T& data);
void pop_back();
void clear();

};
  • miniVector_Impl.h

    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
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    #include"miniVector.h"

    template<typename T>
    void miniVector<T>::memoryExpand(const int elem){

    T* newArr = new T[elem*capacity];
    cout<<"DEBUG: create newArr success"<<endl;
    memcpy(newArr,vectorArr,size*sizeof(T));

    delete[] vectorArr;
    vectorArr = newArr;
    capacity = elem*capacity;
    cout<<"DEBUG: capacity "<< capacity<<endl;

    }


    template<typename T>
    miniVector<T>::miniVector(const int cap){
    capacity = cap;
    size = 0;
    vectorArr = new T[capacity];
    cout<<"LOG: capcity "<<capacity<<endl;
    cout<<"LOG: size "<<size<<endl;

    }


    template<typename T>
    miniVector<T>::miniVector(const int ini_size,T ini_data){

    capacity = 20+ini_size;
    size = ini_size;
    vectorArr = new T[capacity];
    // memset(vectorArr, ini_data,ini_size);
    for(int i=0;i<ini_size;i++)
    {
    vectorArr[i] = ini_data;

    }
    cout<<"LOG: capcity "<<capacity<<endl;
    cout<<"LOG: size "<<size<<endl;


    }

    template<typename T>
    miniVector<T>::~miniVector(){
    delete[] vectorArr;
    vectorArr = NULL;

    }


    template<typename T>
    void miniVector<T>::push_back(const T& num)
    {

    if(vectorArr == NULL)
    {
    capacity = 2;
    size = 0;
    vectorArr = new T[capacity];

    }
    if(size >= capacity)
    {
    memoryExpand(2);//2倍扩容
    }

    vectorArr[size] = num;
    ++size;
    cout<<"DEBUG: size="<<size<<endl;
    cout<<"DEBUG: vectorArr["<<size-1<<"]="<<vectorArr[size-1]<<endl;
    cout<<"DEBUG: capacity"<<capacity<<endl;
    return;

    }


    template<typename T>
    void miniVector<T>::print_info() const
    {
    cout<<"LOG vector capacity: "<< capacity<<endl;
    cout<<"LOG vector size: "<< size<<endl;
    cout<<"LOG vector data: ";
    for(auto i=0;i<size;i++){
    cout<<vectorArr[i]<<" ";
    }
    cout<<endl;
    }



    template<typename T>
    T& miniVector<T>::operator[](const int& index) const
    {
    if(vectorArr == NULL)
    {
    cout<<"Error: Memory Not Initial!"<<endl;

    }

    if( index >= size || index < 0)
    {
    cout<<"Error: Out of Ranger!"<<endl;

    }
    return vectorArr[index];
    }

    template<typename T>
    void miniVector<T>::insert(const int& index,const T& data){
    if(vectorArr == NULL)//如果还没有初始化
    {
    cout<<"Error: Vector is not Initial!"<<endl;
    return;
    }
    if(index >= size-1){
    cout<<"Error: The insert index should be from 0 to "<<size-1<<" "<<endl;
    return;
    }
    if(size>=capacity){
    memoryExpand(2);
    }

    for(int i = size-1;i>= index;i--)
    {
    vectorArr[i+1]=vectorArr[i];
    }
    vectorArr[index]= data;
    ++size;
    cout<<"DEBUG: vectorArr["<<index<<"]="<<vectorArr[index]<<endl;
    return;
    }




    template<typename T>
    void miniVector<T>::pop_back()
    {
    if(vectorArr == NULL || size == 0)
    {
    cout<<"Error: Vector size is 0!"<<endl;
    return;
    }

    --size;
    cout<<"LOG: size "<< size<<endl;
    cout<<"LOG: capacity "<<capacity<<endl;

    }




    template<typename T>
    miniVector<T>::miniVector(const miniVector& vec){
    //深拷贝

    capacity = vec.capacity;
    size = vec.size;
    vectorArr = new T[capacity];
    memcpy(vectorArr,vec.vectorArr,size*sizeof(T));
    cout<<"LOG: now in miniVector(const miniVector& vec) "<<endl;
    cout<<"LOG: size "<<size<<endl;
    cout<<"LOG: capacity "<<capacity<<endl;
    }




    template<typename T>
    miniVector<T>& miniVector<T>::operator=(const miniVector& vec){
    //深拷贝
    if(this == &vec) return *this;

    capacity = vec.capacity;
    size = vec.size;
    delete[] vectorArr;
    vectorArr = new T[capacity];
    memcpy(vectorArr,vec.vectorArr,size*sizeof(T));
    cout<<"LOG: now in miniVector operator= "<<endl;
    cout<<"LOG: size "<<size<<endl;
    cout<<"LOG: capacity "<<capacity<<endl;
    return *this;

    }


    template<typename T>
    void miniVector<T>::clear()
    {
    size=0;
    }
  • mainTest.cpp

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
#include<iostream>
#include"miniVector_impl.h"
using namespace std;

void miniVectorTest_pushback_int(){
miniVector<int> vec;
vec.push_back(1);
vec.push_back(9);
vec.push_back(2);
vec.push_back(4);
vec.push_back(3);
vec.print_info();
cout<<"DEBUG: vec[2] " <<vec[2]<<endl;
vec[13];
}


void miniVectorTest_pushback_char(){
miniVector<char> vec;
vec[1];
vec.push_back('h');
vec.push_back('a');
vec.push_back('p');
vec.push_back('p');
vec.push_back('y');
vec.print_info();
cout<<"DEBUG: vec[2] " <<vec[2]<<endl;

}
void miniVectorTest_insert()
{
miniVector<int> vec(5,2);
vec.insert(0,1);
vec.print_info();
vec.insert(8,1);

}


void miniVectorTest_popback(){
miniVector<int> vec;
vec.pop_back();
miniVector<int> vec1(2,3);
vec.pop_back();
vec.pop_back();
vec.pop_back();

}


void miniVectorTest_copy(){
miniVector<int> vec(2,3);
miniVector<int> vec1(vec);
cout<<"LOG: vec1[1] "<<vec1[1]<<endl;
miniVector<int> vec2 = vec1;
}

void miniVectorTest_copyoperator(){
miniVector<int> vec(2,3);
miniVector<int> vec1;
miniVector<int> vec2;
vec2=vec1 = vec;
cout<<"LOG: vec1[1] "<<vec1[1]<<endl;
cout<<"LOG: vec2[1] "<<vec2[1]<<endl;

}
int main(){

miniVectorTest_copyoperator();


}