-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.cpp
More file actions
164 lines (122 loc) · 3.83 KB
/
Vector.cpp
File metadata and controls
164 lines (122 loc) · 3.83 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
#include<iostream>
#define DEFAULT_SIZE 0
#define RESIZE_FACTOR 2
using namespace std;
class vector {
private:
int *arr{};
int length = 0; // tells the number of items currently stored
int capacity = DEFAULT_SIZE; // how much can we fill the current vector without any resize
void resize(int new_capacity) {
int *temp = new int[new_capacity]; // create an array of new capacity
// Copy all existing elements
for (int i = 0; i < this->capacity; i++) {
temp[i] = arr[i];
}
delete[] arr; // delete old array
arr = temp; // point to new array
this->capacity = new_capacity; // update the variable capacity
}
public:
vector() = default;
vector(int n, int x) {
resize(n); // create an array of supplied size
// set default value on each item
for (int i = 0; i < n; i++) {
arr[i] = x;
length += 1;
}
}
int size() {
return this->length;
}
int &operator[](int);
int get(int index) {
return arr[index];
}
// TODO: handle shrinking as well
void push_back(int x) {
if (length == capacity) { // current array is full
resize(RESIZE_FACTOR * capacity); // make a new array of twice the size
}
arr[length] = x;
length += 1;
}
void pop_back() {
length -= 1; // just decrease the length; effectively removing the last element
}
// Allows inserting at index = length by resizing array.
// Done to imitate vector implementation of STL
// so for eg a user can insert values into an empty vector by using insert instead of push_back()
// but only from the 0th index; incrementing index by one at a time.
void insert(int index, int x) {
if (index > length) {
throw "Index out of bounds.";
}
if (length == capacity) {
resize(RESIZE_FACTOR * capacity);
}
// copy right
for (int i = length; i > index; i--)
arr[i] = arr[i - 1];
// insert at given index
arr[index] = x;
length += 1; // One more item added now
}
void erase(int index) {
for (int i = index; i < length - 1; i++)
arr[i] = arr[i + 1];
length -= 1; // decrease number of elements by 1
}
int front() {
return arr[0];
}
int back() {
return arr[length - 1];
}
void print_data() {
cout << endl;
for (int i = 0; i < this->length; i++)
cout << arr[i] << " ";
}
};
int &vector::operator[](int index) {
if (index >= length or index < 0)
throw "Index out of bounds.";
return arr[index];
}
int main() {
/**
* Driver code
*/
vector v = vector(); //object for class vector
cout << "\nSize of blank vector: " << v.size() << endl;
v = vector(10, 40);
cout << "\nSize of new vector with default value of 40: " << v.size() << endl;
cout << "\nValue at index 5: " << v[5];
cout << "\nPushing values 1,2,4,5 at the end";
v.push_back(1);
v.push_back(2);
v.push_back(4);
v.push_back(5);
cout << "\nVector contents after push_back():-\n";
v.print_data();
cout << "\nPoping twice from back";
v.pop_back();
v.pop_back();
cout << "\nVector contents after pop_back():-\n";
v.print_data();
cout << "\nInserting 10 at index 0\n";
v.insert(0, 10);
cout << "\nInserting 20 at index 2\n";
v.insert(2, 20);
cout << "\nVector contents before erase():-\n";
v.print_data();
cout << "\nErasing item at index 2\n";
v.erase(2);
cout << "\nVector content after erase():-\n";
v.print_data();
cout << "\nElement at front:" << v.front();
cout << "\nElement at back:" << v.back();
return 0;
}