-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstd_vector.h
More file actions
76 lines (63 loc) · 1.09 KB
/
std_vector.h
File metadata and controls
76 lines (63 loc) · 1.09 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
#pragma once
#include "iterator.h"
class vector
{
private:
double* data = nullptr;
int n = 0;
int capacity = 0;
int factor = 2 * n;//amount of memory allocated in the constructor
public:
vector():n(0),capacity(1)
{
data = new double[capacity];
}
vector(int n,double elem = 0):n(n)
{
capacity = factor;
data = new double[capacity];
for (int i = 0; i < this->n; i++)
{
data[i] = elem;
}
}
vector(double* arr,int n) :n(n)
{
capacity = factor;
data = new double[capacity];
for (int i = 0; i < n; i++)
{
data[i] = arr[i];
}
}
vector(const vector& v) : n(v.n),capacity(v.capacity)
{
this->data = new double[n];
for (int i = 0; i < n; i++)
{
data[i] = v.data[i];
}
}
iterator begin()
{
return data;
}
iterator end()
{
return (data + n);
}
void print() const;
double& operator[](int i);
vector& operator=(const vector& v);
vector& push_back(double elem);
vector& pop_back();
vector& insert(double elem, int pos);
vector& erase(int pos);
int size() const;
int capacity_size() const;
bool empty() const;
~vector()
{
delete[] data;
}
};