-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.h
190 lines (172 loc) · 4.95 KB
/
Vector.h
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* This class represents a dynamic array (vector) that can grow in size as needed.
* It supports basic operations such as resizing, accessing elements, and displaying the contents.
*/
#pragma once
#include <iostream>
#include <stdexcept>
template <typename T>
class Vector
{
T *data; // Pointer to the array of elements
size_t cap; // Capacity of the vector (maximum number of elements it can hold)
size_t len; // Current size of the vector (number of elements currently stored)
public:
/**a
* @brief Constructor to initialize vector with a specified size.
*
* If an initial size is provided, the vector is created with that many elements, each default-initialized.
* If no size is provided, an empty vector is created.
*
* @param initial_size The initial size of the vector (default is 0).
*/
Vector(size_t initial_size = 0)
: cap(initial_size), len(initial_size), data(initial_size ? new T[initial_size] : nullptr) {}
/**
* @brief Constructor to initialize a vector with a list of values.
*
* This constructor creates a vector with the specified elements from an initializer list.
*
* @param list An initializer list of elements.
*/
Vector(std::initializer_list<T> list)
: cap(list.size()), len(list.size()), data(new T[list.size()])
{
size_t i = 0;
for (const T &elem : list)
{
data[i++] = elem;
}
}
/**
* @brief Destructor to clean up memory allocated for the vector.
*
* The destructor frees the dynamically allocated memory for the vector's elements.
*/
~Vector()
{
if (len)
{
len = 0;
delete[] data;
}
}
/**
* @brief Access element at the specified index.
*
* This function returns a reference to the element at the specified index. If the index is out of bounds,
* an exception is thrown.
*
* @param index The index of the element to access.
* @return A reference to the element at the specified index.
* @throws std::out_of_range If the index is out of bounds.
*/
T &operator[](size_t index) const
{
if (index >= len)
{
throw std::out_of_range("Index out of bounds in Vector::operator[]");
}
return data[index];
}
/**
* @brief Resize the vector to a new size.
*
* This function resizes the vector to the specified size. If the new size is greater than the current capacity,
* the capacity is increased. New elements are default-initialized if the new size is larger than the current size.
*
* @param new_size The new size for the vector.
*/
void resize(size_t new_size)
{
if (new_size > cap)
{
// If the new size exceeds the current capacity, allocate new memory
T *new_data = new T[new_size];
for (size_t i = 0; i < len; ++i)
{
new_data[i] = data[i]; // Copy existing elements to the new array
}
delete[] data; // Free the old array
data = new_data; // Point to the new array
cap = new_size; // Update the capacity
}
// Default-initialize new elements if resizing to a larger size
for (size_t i = len; i < new_size; ++i)
{
data[i] = T();
}
len = new_size; // Update the current size
}
/**
* @brief Get the current size of the vector.
*
* This function returns the current size of the vector (the number of elements stored in the vector).
*
* @return The number of elements currently stored in the vector.
*/
size_t size() const
{
return len;
}
/**
* @brief Display the elements of the vector.
*
* This function prints all the elements of the vector to the console.
*/
void display(bool printFirstElement = true, std::string sep = " ")
{
if (printFirstElement)
{
std::cout << data[0];
std::cout << sep;
}
for (int i = 1; i < len - 1; ++i)
std::cout << data[i] << sep;
if (len > 0)
std::cout << data[len - 1];
std::cout << std::endl;
}
/**
* @brief Add a new element to the end of the vector.
*
* If the current capacity is not sufficient, the vector's capacity is doubled before adding the new element.
*
* @param value The value to be added to the vector.
*/
void push_back(const T &value)
{
resize(len + 1);
data[len++] = value;
}
int findOne(const T &key) const
{
for (int i = 0; i < len; ++i)
{
if (data[i] == key)
return i;
}
return -1;
}
/**
* @brief Remove an element by its index.
*
* This function removes the element at the specified index and shifts the subsequent elements
* to fill the gap. If the index is invalid, an exception is thrown.
*
* @param index The index of the element to remove.
* @throws std::out_of_range If the index is out of bounds.
*/
void removeByIndex(int index)
{
if (index >= len)
{
throw std::out_of_range("Index out of bounds in Vector::removeByIndex");
}
for (int i = index; i < len - 1; ++i)
{
data[i] = data[i + 1];
}
--len;
}
};