-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyVector.h
More file actions
226 lines (226 loc) · 5.73 KB
/
MyVector.h
File metadata and controls
226 lines (226 loc) · 5.73 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#pragma once
#pragma once
#include <iostream>
using namespace std;
template<typename T>
class MyVector {
private:
T *m_date;
unsigned int m_size;
bool unsort;
public:
~MyVector() { delete[]this->m_date; };
MyVector(unsigned int siz = 0) :m_size(siz), unsort(true) {
this->m_date = nullptr;
};
MyVector(unsigned int siz, const T &n) :m_size(siz), unsort(true) {
this->m_size = siz;
this->m_date = new T[siz];
for (unsigned int i = 0; i < siz; i++)
{
m_date[i] = n;
}
};
MyVector(const MyVector& myvector) {
this->m_size = myvector.m_size;
this->unsort = myvector.unsort;
this->m_date = new T[myvector.m_size];
for (unsigned int i = 0; i < myvector.m_size; i++)
{
m_date[i] = myvector.m_date[i];
}
};
MyVector & operator=(const MyVector&myvector) {
if (this->date) delete[]date;
this->unsort = myvector.unsort;
this->m_size = myvector.m_size;
for (unsigned int i = 0; i <myvector.m_size; i++)
{
m_date[i] = myvector.m_date[i];
}
return *this;
}
inline const unsigned int getsize() const {
return this->m_size;
}
void resize(unsigned int siz, const T& t) {
if (siz == m_size) return;
T *date = new T[m_size];
if (this->m_date) {
for (unsigned int i = 0; i < m_size; i++) {
date[i] = m_date[i];
}
delete[]m_date;
m_date = nullptrptr;
}
this->m_date = new T[siz];
if (siz > this->m_size) {
for (unsigned int i = m_size; i < siz; i++)
m_date[i] = t;
for (unsigned int i = 0; i < m_size; i++)
m_date[i] = date[i];
}
else {
for (unsigned int i = 0; i < siz; i++)
m_date[i] = date[i];
}
this->m_size = siz;
delete[]date;
date = nullptrptr;
}
void resize(unsigned int siz) {
if (siz == m_size) return;
T *date = new T[m_size];
if (this->m_date) {
for (unsigned int i = 0; i < m_size; i++) {
date[i] = m_date[i];
}
delete[]m_date;
m_date = nullptr;
}
this->m_date = new T[siz];
if (siz > this->m_size) {
for (unsigned int i = 0; i < m_size; i++)
m_date[i] = date[i];
}
else {
for (unsigned int i = 0; i < siz; i++)
m_date[i] = date[i];
}
this->m_size = siz;
delete[]date;
date = nullptr;
}
inline bool empty()const {
return (this->m_size == 0) ? true : false;
}
inline T & operator[](unsigned int t) {
return m_date[t];
}
inline const T& operator[](unsigned int t)const {
return m_date[t];
}
T& at(unsigned int t) {
if (t >= m_size) throw out_of_range("out_of_range");
else return m_date[t];
}
const T& at(unsigned int t)const {
if (t >= m_size) throw out_of_range("out_of_range");
else return m_date[t];
}
T &back() {
if (this->m_size == 0) throw length_error("length_error");
return m_date[m_size - 1];
}
const T&back()const {
if (this->m_size == 0) throw length_error("length_error");
return m_date[m_size - 1];
}
T &front() {
if (this->m_size == 0) throw length_error("length_error");
return m_date[0];
}
const T &front()const {
if (this->m_size == 0) throw length_error("length_error");
return m_date[0];
}
void push_back(const T& t) {
this->resize(this->m_size + 1);
m_date[m_size - 1] = t;
}
void push_back(T& t) {
this->resize(this->m_size + 1);
m_date[m_size - 1] = t;
}
void pop_back() {
if (this->m_size == 0) throw length_error("length_error");
this->resize(m_size - 1);
}
void insert(unsigned int i, const T& t) {
if (this->m_size == 0) throw length_error("length_error");
if (this->m_size<i) throw length_error("length_error");
this->resize(this->m_size + 1);
for (unsigned int j = m_size - 1; j > i; j--)
m_date[j] = m_date[j - 1];
m_date[i] = t;
}
void insert(unsigned int i, int num, const T& t) {
if (this->m_size == 0) throw length_error("length_error");
if (this->m_size<i) throw length_error("length_error");
this->resize(num + this->m_size);
for (unsigned int j = m_size - num - 1; j >= i; j--) {
m_date[j + num] = m_date[j];
if (j == i) break;
}
for (unsigned int k = i; k < i + num; k++) {
m_date[k] = t;
}
}
void erase(unsigned int i) {
if (this->m_size == 0) throw length_error("length_error");
if (this->m_size<i) throw length_error("length_error");
for (unsigned int j = i; j < m_size - 1; j++)
m_date[j] = m_date[j + 1];
this->resize(this->m_size - 1);
}
void clear() {
this->m_size = 0;
}
void sort() {
if (this->unsort == false) return;
T in;
for (unsigned int i = 0; i < m_size; i++)
for (unsigned int j = 0; j < m_size - i - 1; j++) {
if (m_date[j] > m_date[j + 1]) {
in = m_date[j];
m_date[j] = m_date[j + 1];
m_date[j + 1] = in;
}
}
this->unsort = false;
}
int binarySearch(const T& key) {
if (this->m_size == 0) throw length_error("length_error");
if (this->unsort == true) this->sort();
int high = m_size - 1, low = 0, mid = -1;
while (low <= high) {
mid = (low + high) / 2;
if (key < m_date[mid]) high = mid - 1;
else if (key > m_date[mid]) low = mid + 1;
else {
return mid;
}
}
if (!(m_date[mid] == key)) mid = -1;
return mid;
}
void show() {
for (unsigned int i = 0; i < m_size; i++) {
cout << m_date[i] <<endl;
//if (i == m_size - 1) cout << endl;
}
}
private:
void quickSort(int, int);
};
template<typename T>
void MyVector<T>::quickSort(int l, int r)
{
if (this->m_size == 0) throw length_error("length_error");
if (l < r) {
int i = l, j = r, x = m_data[l];
while (i < j) {
while (i < j&&m_data[j] >= x)
j--;
if (i < j)
m_data[i++] = m_data[j];
while (i < j&&m_data[i] < x)
i++;
if (i < j)
m_data[j--] = data[i];
}
m_data = x;
quickSort(l, i - 1);
quickSort(i + 1, r);
}
}