-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_array.cpp
More file actions
69 lines (67 loc) · 1.1 KB
/
Copy pathvector_array.cpp
File metadata and controls
69 lines (67 loc) · 1.1 KB
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
/*
* 2019.6.24
* data structure implementation - vector_array
*/
using namespace std;
class Vector {
public:
int capacity, v_size;
int* vector;
Vector() {
capacity = 8;
v_size = 0;
vector = new int[capacity];
}
~Vector() {
delete[] vector;
}
void push_back(int value) {
if (capacity == v_size)
{
capacity *= 2;
int* temp = new int[capacity];
for (int i = 0; i < v_size; i++)
{
temp[i] = vector[i];
}
delete vector;
vector = temp;
}
vector[v_size++] = value;
}
void pop_back() {
if (v_size == 0)
{
return;
}
v_size--;
}
int size() {
return v_size;
}
bool empty() {
return !v_size;
}
void clear() {
capacity = 8;
v_size = 0;
delete[] vector;
vector = new int[capacity];
}
int &operator[](int index) {
return vector[index];
}
int &operator[](const int &index)const {
return vector[index];
}
void operator=(const Vector &temp_vector) {
capacity = temp_vector.capacity;
v_size = temp_vector.v_size;
delete[] vector;
vector = new int[capacity];
for (int i = 0; i < v_size; i++)
{
vector[i] = temp_vector[i];
}
}
};