-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorDouble.cpp
More file actions
68 lines (58 loc) · 1.33 KB
/
VectorDouble.cpp
File metadata and controls
68 lines (58 loc) · 1.33 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
#include "VectorDouble.h"
Vector& Vector::operator=(const Vector& arg) // copy operator &
{
if(this == &arg) return *this;
if(arg.sz <= space)
{
for(int i = 0; i < arg.sz; ++i) elem[i] = arg.elem[i];
sz = arg.sz;
return *this;
}
double *p = new double[arg.sz];
for(int i = 0; i < arg.sz; ++i) p[i] = arg.elem[i];
delete[] elem;
space = sz = arg.sz;
elem = p;
return *this;
}
Vector& Vector::operator=(Vector&& arg) // move operator &&
{
delete[] elem;
elem = arg.elem;
sz = arg.sz;
space = arg.space;
arg.elem = nullptr;
arg.sz = 0;
arg.space = 0;
return *this;
}
void Vector::reserve(int newalloc)
{
if(newalloc <= space) return;
double *p = new double[newalloc];
for(int i = 0; i < sz; ++i) p[i] = elem[i];
delete[] elem;
elem = p;
space = newalloc;
}
void Vector::resize(int newsize)
{
reserve(newsize);
for(int i = sz; i < newsize; ++i) elem[i] = 0;
sz = newsize;
}
void Vector::push_back(double d)
{
if(space == 0)
reserve(8);
else if(sz==space)
reserve(2*space);
elem[sz] = d;
++sz;
}
void printer(Vector& v)
{
for(int i = 0; i < v.size(); ++i)
std::cout << v[i] << "\t";
std::cout << "\n";
}