-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVectorString.cpp
148 lines (126 loc) · 4.81 KB
/
VectorString.cpp
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
//VectorString.cpp
#include "VectorString.h"
//default constructor
pic10b::VectorString::VectorString() : vec_size(0), vec_capacity(1), stringPointer(std::make_unique<std::string[]>(vec_capacity)) {}
//size_t constructor
pic10b::VectorString::VectorString(size_t size) : vec_size(size), vec_capacity(2 * size), stringPointer(std::make_unique<std::string[]>(vec_capacity)) {
//for loop to set vector elements to empty string
for (size_t i = 0; i < vec_size; ++i) {
stringPointer[i] = "";
}
}
//size and string constructor
pic10b::VectorString::VectorString(size_t size, const std::string& inputString)
: vec_size(size), vec_capacity(2 * size), stringPointer(std::make_unique<std::string[]>(vec_capacity)) {
//for loop to set vector elements to inputted string
for (size_t i = 0; i < vec_size; ++i) {
stringPointer[i] = inputString;
}
}
//copy constructor
pic10b::VectorString::VectorString(const VectorString& copyVector)
: vec_size(copyVector.vec_size), vec_capacity(copyVector.vec_capacity), stringPointer(std::make_unique<std::string[]>(vec_capacity)) {
//copy over values of each element to new vector
for (size_t i = 0; i < vec_size; ++i) {
stringPointer[i] = copyVector.stringPointer[i];
}
}
//move constructor
pic10b::VectorString::VectorString(VectorString&& oldVector) noexcept : VectorString() {
//invoke default constructor (above) and then swap all values
std::swap(vec_size, oldVector.vec_size);
std::swap(vec_capacity, oldVector.vec_capacity);
std::swap(stringPointer, oldVector.stringPointer);
}
//copy assignment operator
pic10b::VectorString& pic10b::VectorString::operator = (const VectorString& oldVector) {
//copy as long as vectors aren't already the same
if (this != &oldVector) {
//copy size and capacity
vec_size = oldVector.vec_size;
vec_capacity = oldVector.vec_capacity;
//make new string pointer and copy over old values
stringPointer = std::make_unique<std::string[]>(vec_capacity);
for (size_t i = 0; i < vec_size; ++i) {
stringPointer[i] = oldVector.stringPointer[i];
}
}
return *this;
}
//move assignment operator
pic10b::VectorString& pic10b::VectorString::operator = (VectorString&& oldVector) noexcept {
//move as long as vectors aren't already the same
if (this != &oldVector) {
//swap all values
std::swap(vec_size, oldVector.vec_size);
std::swap(vec_capacity, oldVector.vec_capacity);
std::swap(stringPointer, oldVector.stringPointer);
}
return *this;
}
//size function
size_t pic10b::VectorString::size() const {
return vec_size;
}
//capacity function
size_t pic10b::VectorString::capacity() const {
return vec_capacity;
}
//push back function
void pic10b::VectorString::push_back(const std::string& inputString) {
//check if the capacity is equal to the size, meaning the vector is full
if (vec_size == vec_capacity) {
//if it's full, then double the capacity
vec_capacity *= 2;
//now, make a temp pointer to store the old values and update the original pointer to a new pointer with larger capacity
auto stringPointerTemp = stringPointer.release();
stringPointer = std::make_unique<std::string[]>(vec_capacity);
//for loop to copy temp values back into the original pointer
for (size_t i = 0; i < vec_size; ++i) {
stringPointer[i] = stringPointerTemp[i];
}
}
//after checking if full - and if so, updating the capacity - insert the new value and update the size accordingly
stringPointer[vec_size] = inputString;
++vec_size;
}
//pop back function
void pic10b::VectorString::pop_back() {
//decrement the size by 1 so that the last value is "removed"
--vec_size;
}
//deleteAt function
void pic10b::VectorString::deleteAt(const size_t& index) {
//for loop to iterate over indices and shift them back, deleting given index as well
for (size_t i = index; i < vec_size; ++i) {
stringPointer[i] = stringPointer[i + 1];
}
//decrement size by 1 to account for deletion
--vec_size;
}
//insertAt function
void pic10b::VectorString::insertAt(const size_t& index, const std::string& inputString) {
//check if vector is full
if (vec_size == vec_capacity) {
//if it's full, then double the capacity
vec_capacity *= 2;
//now, make a temp pointer to store the old values and update the original pointer to a new pointer with larger capacity
auto stringPointerTemp = stringPointer.release();
stringPointer = std::make_unique<std::string[]>(vec_capacity);
//for loop to copy temp values back into the original pointer
for (size_t i = 0; i < vec_size; ++i) {
stringPointer[i] = stringPointerTemp[i];
}
}
//after checking if full, push forward all old elements
for (size_t i = vec_size; i > index; --i) {
stringPointer[i] = stringPointer[i - 1];
}
//insert new string and increment size to accomodate
stringPointer[index] = inputString;
++vec_size;
}
//at function
std::string& pic10b::VectorString::at(const size_t& index) const {
return stringPointer[index];
}