Skip to content

Commit 956bd8c

Browse files
committed
update Vector.h
1 parent bbfdf0a commit 956bd8c

File tree

6 files changed

+151
-9
lines changed

6 files changed

+151
-9
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ Remember that each data has its own trade-offs. And you need to pay attention mo
2222
`B` - Beginner, `A` - Advanced
2323

2424
* `B` [Array](data-structures/Array)
25-
* `B` [Static Array](data-structures/Array/include/Array.h)
26-
* `B` [Dynamic Array](data-structures/Array/include/Vector.h)
25+
* `B` Static Array
26+
* `B` Dynamic Array
2727
* `B` [Linked List](data-structures/LinkedList)
28-
* `B` [Singly Linked List](data-structures/LinkedList/include/SinglyLinkedList.h)
29-
* `B` [Doubly Linked List](data-structures/LinkedList/include/DoublyLinkedList.h)
28+
* `B` Singly Linked List
29+
* `B` Doubly Linked List
3030
* `B` [Queue](data-structures/Queue)
3131
* `B` [Stack](data-structures/Stack)
3232

README.zh-CN.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
`B` - 初学者, `A` - 进阶
2020

2121
* `B` [数组](data-structures/Array)
22-
* `B` [单链表](data-structures/LinkedList)
23-
* `B` [双链表](data-structures/DoublyLinkedList)
22+
* `B` 静态数组
23+
* `B` 动态数组
24+
* `B` [链表](data-structures/LinkedList)
25+
* `B` 单向链表
26+
* `B` 双向链表
2427
* `B` [队列](data-structures/Queue)
2528
* `B` [](data-structures/Stack)
2629

data-structures/Array/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# 数组
22

3-
![数组](./assets/array.png)
3+
![数组](./assets/array.png)
4+
5+
![Vector](./assets/vector.png)

data-structures/Array/__test__/test_Array.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
#include "../include/Array.h"
21
#include <cassert>
32
#include <iostream>
3+
#include "../include/Array.h"
4+
#include "../include/Vector.h"
45

5-
void test_array() {
6+
void test_array(){
7+
std::cout << "*****Array Test Begin*****" << std::endl;
68
Array<int, 0> arr_0;
79
assert(arr_0.size() == 0);
810
assert(arr_0.empty() == true);
@@ -41,9 +43,20 @@ void test_array() {
4143
}
4244
std::cout << std::endl;
4345
std::cout << "All tests passed!" << std::endl;
46+
std::cout << "*****Array Test End*****" << std::endl;
47+
}
48+
49+
void test_vector(){
50+
std::cout << "*****Vector Test Begin*****" << std::endl;
51+
52+
53+
54+
std::cout << "*****Vector Test End*****" << std::endl;
4455
}
4556

4657
int main() {
4758
test_array();
59+
std::cout << std::endl;
60+
test_vector();
4861
return 0;
4962
}
90.3 KB
Loading
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,129 @@
11
#ifndef VECTOR_H
22
#define VECTOR_H
33

4+
#include <cstddef>
5+
#include <stdexcept>
6+
7+
template <typename T>
8+
class Vector{
9+
public:
10+
Vector();
11+
Vector(size_t N);
12+
Vector(size_t N, const T& value);
13+
~Vector();
14+
public:
15+
// Capacity
16+
bool empty() const {return _start == _finish;}
17+
size_t size() const {return static_cast<size_t>(_finish - _start);};
18+
size_t capacity() const {return static_cast<size_t>(_end_of_storage - _start);};
19+
void shrink_to_fit();
20+
// Element access
21+
T& front();
22+
T& back();
23+
T& at(const size_t index);
24+
T& operator[](const size_t index);
25+
// Modifiers
26+
void clear();
27+
void resize(const size_t count);
28+
void insert(const size_t index);
29+
void erase(const size_t index);
30+
void push_back(const T &value);
31+
void pop_back();
32+
private:
33+
void _reallocate(const size_t new_capacity);
34+
private:
35+
T *_start;
36+
T *_finish;
37+
T *_end_of_storage;
38+
};
39+
40+
//Constructor and Destructor
41+
template <typename T>
42+
Vector<T>::Vector(){
43+
_start = nullptr;
44+
_finish = nullptr;
45+
_end_of_storage = nullptr;
46+
}
47+
48+
template <typename T>
49+
Vector<T>::Vector(size_t N){
50+
if(N > 0){
51+
_start = new T[N];
52+
_finish = _start;
53+
_end_of_storage = _start + N;
54+
}
55+
else{
56+
_start = nullptr;
57+
_finish = nullptr;
58+
_end_of_storage = nullptr;
59+
}
60+
}
61+
62+
template <typename T>
63+
Vector<T>::Vector(size_t N, const T &value){
64+
if(N > 0){
65+
_start = new T[N];
66+
_finish = _start + N;
67+
_end_of_storage = _finish;
68+
for(size_t i = 0; i < N; ++i){
69+
_start[i] = value;
70+
}
71+
}
72+
else{
73+
_start = nullptr;
74+
_finish = nullptr;
75+
_end_of_storage = nullptr;
76+
}
77+
}
78+
79+
template <typename T>
80+
Vector<T>::~Vector(){
81+
delete[] _start;
82+
_start = nullptr;
83+
_finish = nullptr;
84+
_end_of_storage = nullptr;
85+
}
86+
87+
template <typename T>
88+
void Vector<T>::shrink_to_fit(){
89+
if(_finish == _end_of_storage){
90+
return;
91+
}
92+
while(_end_of_storage != _finish){
93+
T* del = _end_of_storage - 1;
94+
delete del;
95+
--_end_of_storage;
96+
}
97+
}
98+
99+
// Elements access
100+
template <typename T>
101+
T& Vector<T>::front(){
102+
if(_start == _finish){
103+
throw std::underflow_error("front(): This Vector is empty.");
104+
}
105+
return _start[0];
106+
}
107+
108+
template <typename T>
109+
T &Vector<T>::back(){
110+
if(_start == _finish){
111+
throw std::underflow_error("back(): This Vector is empty.");
112+
}
113+
return *(_finish - 1);
114+
}
115+
116+
template <typename T>
117+
T &Vector<T>::at(const size_t index){
118+
if(index >= size()){
119+
throw std::out_of_range("at(): Index out of range.");
120+
}
121+
return _start[index];
122+
}
123+
124+
template <typename T>
125+
T& Vector<T>::operator[](const size_t index){
126+
return _start[index];
127+
}
4128

5129
#endif // VECTOR_H

0 commit comments

Comments
 (0)