-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.cpp
More file actions
52 lines (46 loc) · 864 Bytes
/
Vector.cpp
File metadata and controls
52 lines (46 loc) · 864 Bytes
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
#include "Vector.h"
#include "Exception.h"
using namespace DS;
Vector::Vector(int size) : size(size), data(new int[size])
{
for (int i = 0; i < size; i++)
{
data[i] = 0;
}
}
void Vector::insert(int i, int val)
{
data[i] = val;
}
int &Vector::operator[](int i)
{
if (i >= size)
{
throw Exception("i is greater then the size of the vector", INVALID_INPUT);
}
return data[i];
}
Vector::Vector(const Vector &origin)
{
size = origin.size;
data = new int[size];
for (int i = 0; i < size; i++)
{
data[i] = origin.data[i];
}
}
Vector& Vector::operator=(const Vector &origin)
{
delete[] data;
size = origin.size;
data = new int[size];
for (int i = 0; i < size; i++)
{
data[i] = origin.data[i];
}
return *this;
}
Vector::~Vector()
{
delete[] data;
}