1+ /*
2+ Vector.h
3+ STL中的vector的实现:使用数组实现列表
4+ author: Ye Hu
5+ 2016/10/08
6+ */
7+
8+ #ifndef VECTOR_H_
9+ #define VECTOR_H_
10+
11+ #include < algorithm>
12+
13+ template <typename Object>
14+ class Vector
15+ {
16+ public:
17+ // 禁止隐式转换
18+ explicit Vector (int initSize = 0 ) : theSize{ initSize },
19+ theCapacity {initSize + SPARE_CAPACITY}
20+ {
21+ objects = new Object[theCapacity];
22+ }
23+
24+ // copy构造函数
25+ Vector (const Vector& rhs) : theSize{ rhs.theSize }, theCapacity{ rhs.theCapacity },
26+ objects{nullptr }
27+ {
28+ objects = new Object[theCapacity];
29+ // 浅copy
30+ for (int k = 0 ; k < theSize; k++)
31+ {
32+ objects[k] = rhs.objects [k];
33+ }
34+ }
35+ // 析构函数
36+ ~Vector ()
37+ {
38+ delete[] objects;
39+ }
40+
41+ // move构造函数
42+ Vector (Vector && rhs) :theSize{ rhs.theSize }, theCapacity{ rhs.theCapacity },
43+ objects{rhs.objects }
44+ {
45+ rhs.objects = nullptr ;
46+ rhs.theCapacity = 0 ;
47+ rhs.theSize = 0 ;
48+ }
49+
50+ // 重载赋值操作符
51+ Vector& operator =(const Vector& rhs)
52+ {
53+ Vector copy = rhs; // 调用copy构造函数
54+ std::swap (*this , copy); // 内部使用move构造函数和move赋值操作符
55+ return *this ;
56+ }
57+ // move赋值操作符
58+ Vector& operator =(Vector&& rhs)
59+ {
60+ // 利用移动语义
61+ std::swap (theSize, rhs.theSize );
62+ std::swap (theCapacity, rhs.theCapacity );
63+ std::swap (objects, rhs.objects );
64+ return *this ;
65+ }
66+
67+ // 重置大小
68+ void resize (int newSize)
69+ {
70+ if (newSize > theCapacity)
71+ {
72+ reserve (newSize * 2 );
73+ }
74+ theSize = newSize;
75+ }
76+
77+ void reserve (int newCapacity)
78+ {
79+ if (newCapacity < theSize)
80+ {
81+ return ; // 无作用
82+ }
83+ Object* newArray = new Object[newCapacity];
84+ for (int k = 0 ; k < theSize; ++k)
85+ {
86+ newArray[k] = std::move (objects[k]); // 获取右值,将会使用move赋值函数,避免重复赋值对象
87+ }
88+ theCapacity = newCapacity;
89+ std::swap (objects, newArray); // 交换两个指针的指向
90+ delete[] newArray; // 实际删除的旧的
91+ }
92+
93+ // []运算符
94+ Object& operator [](int index)
95+ {
96+ return objects[index];
97+ }
98+ const Object& operator [](int index) const // const Vector对象使用
99+ {
100+ return objects[index];
101+ }
102+
103+ // 是否为空
104+ bool empty () const { return size () == 0 ; }
105+ // 大小
106+ int size () const { return theSize; }
107+ // 当前容量
108+ int capacity () const { return theCapacity; }
109+
110+ // 末尾插入元素
111+ void push_back (const Object& x)
112+ {
113+ if (theSize == theCapacity)
114+ {
115+ reserve (2 * theCapacity + 1 ); // 扩张容量
116+ }
117+ objects[theSize++] = x;
118+ }
119+
120+ // 末尾插入元素(使用右值)
121+ void push_back (Object && x)
122+ {
123+ if (theSize == theCapacity)
124+ {
125+ reserve (2 * theCapacity + 1 );
126+ }
127+ objects[theSize++] = std::move (x);
128+ }
129+
130+ // 删除末尾元素
131+ void pop_back ()
132+ {
133+ --theSize;
134+ }
135+
136+ // 返回最后一个元素
137+ const Object& back () const
138+ {
139+ return objects[theSize - 1 ];
140+ }
141+
142+ // 返回第一个元素
143+ const Object& front () const
144+ {
145+ return objects[0 ];
146+ }
147+
148+ // 定义迭代器
149+ typedef Object* iterator;
150+ typedef const Object* const_iterator;
151+
152+ // 初始元素位置
153+ iterator begin ()
154+ {
155+ return &objects[0 ];
156+ }
157+ const_iterator begin () const
158+ {
159+ return &objects[0 ];
160+ }
161+
162+ // 末尾元素位置
163+ iterator end ()
164+ {
165+ return &objects[size ()];
166+ }
167+
168+ const_iterator end () const
169+ {
170+ return &objects[size ()];
171+ }
172+
173+
174+ static const int SPARE_CAPACITY = 16 ; // 备用容量
175+ private:
176+ int theSize; // 实际存储元素大小
177+ int theCapacity; // 当前容量
178+ Object* objects; // 存储数组
179+ };
180+ #endif
0 commit comments