0%

自己动手实现vector

有了实现string的基础,在加上一点点模板的知识,就可以自己动手实现一个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
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
#ifndef MY_VECTOR_H
#define MY_VECTOE_H
#include<cassert>
typedef unsigned int size_t;
template < class T>
class Vector {
public:
typedef T * iterator;

Vector();
Vector(int size, T const& a);
Vector(const Vector<T> & a);
~Vector();
size_t size() const;
size_t capacity() const;
size_t max_capacity() const;
void push_back(const T& val);
void pop_back();
T& operator[](int index);
Vector<T>& operator=(const Vector<T>& a);
bool empty() const;
iterator begin() const;
iterator end() const;
private:
size_t _size;
size_t _capacity;
T* _buf;
const size_t _max_capacity = 65536;
};
template<class T>
Vector<T>::Vector()
{
_size = 0;
_buf = new T[1];
_capacity = 1;
}

template<class T>
Vector<T>::Vector(int s, const T& a)
{
if (s > _max_capacity) {
s = _max_capacity;
}
_size = s;
_capacity = 1;
while (_capacity < _size) {
_capacity *= 2;
}
_buf = new T[_capacity];
for (size_t i = 0; i < _size; i++) {
_buf[i] = a;
}
}
template<class T>
Vector<T>::Vector(const Vector<T> & a)
{
_size = a._size;
_capacity = a._capacity;
_buf = new T[_capacity];
for (size_t i = 0; i < _size; i++) {
_buf[i] = a._buf[i];
}
}
template<class T>
Vector<T>::~Vector()
{
delete[] _buf;
}

template<class T>
size_t Vector<T>::size() const
{
return _size;
}

template<class T>
size_t Vector<T>::capacity() const
{
return _capacity;
}
template<class T>
size_t Vector<T>::max_capacity() const
{
return _max_capacity;
}
template<class T>
T& Vector<T>::operator[](int index)
{
assert(index >= 0 && index < _size);
return _buf[index];
}

template<class T>
void Vector<T>::push_back(const T& val)
{
if (_size < _capacity) {
_buf[_size] = val;
_size++;
return ;
} else if (_size == _max_capacity) {
return ;
}
_capacity *= 2;
if (_capacity >= _max_capacity) {
_capacity = _max_capacity;
}
T * tmp = new T[_capacity];
for (size_t i = 0; i < _size; i++) {
tmp[i] = _buf[i];
}
tmp[_size] = val;
_size++;
delete[] _buf;
_buf = tmp;
}

template<class T>
void Vector<T>::pop_back()
{
assert(_size > 0);
_size--;
}
template<class T>
bool Vector<T>::empty() const
{
if (_size == 0) {
return true;
}
return false;
}
// 迭代器的实现
template<class T>
typename Vector<T>::iterator Vector<T>::begin() const
{
return _buf;
}
template<class T>
typename Vector<T>::iterator Vector<T>::end() const
{
return _buf + _size;
}
template<class T>
Vector<T>& Vector<T>::operator=(const Vector<T> & a)
{
if (this == &a) {
return *this ;
}
delete[] _buf;
_size = a._size;
_capacity = a._capacity;
_buf = new T[_capacity];
for (size_t i = 0; i < _size; i++) {
_buf[i] = a._buf[i];
}
return *this;
}
#endif

测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include "myvector.h"
#include<string>
using namespace std;
int main()
{
int c = 20;
Vector<string> a;
Vector<string> b;
for (int i = 0 ; i < 3; i++) {
a.push_back("hello");
}
b = a;
for (Vector<string>::iterator it = b.begin(); it != b.end(); it++) {
cout << *it << " " << endl;
}
return 0;
}

运行结果:

1
2
3
4
$ ./a.out
hello
hello
hello