Repository navigation
Expand file tree
/
Copy pathCustomVector.h
More file actions
128 lines (109 loc) · 2.79 KB
/
Copy pathCustomVector.h
File metadata and controls
128 lines (109 loc) · 2.79 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
#ifndef CUSTOMVECTOR_H
#define CUSTOMVECTOR_H
#include <cstddef> // For size_t
#include <stdexcept> // For std::out_of_range exception
template <typename T>
class CustomVector {
private:
T* data;
size_t _size;
size_t _capacity;
public:
explicit CustomVector(size_t initial_capacity = 3);
~CustomVector();
void push_back(const T& value);
void pop_back();
T& operator[](size_t index);
T& at(size_t index);
[[nodiscard]] size_t size() const;
[[nodiscard]] size_t capacity() const;
void resize(size_t new_capacity);
[[nodiscard]] bool empty() const;
void clear();
T& front();
T& back();
};
// Constructor
template <typename T>
CustomVector<T>::CustomVector(size_t initial_capacity)
: _size(0), _capacity(initial_capacity) {
data = new T[_capacity];
}
// Destructor
template <typename T>
CustomVector<T>::~CustomVector() {
delete[] data;
}
// Add an element to the end of the vector
template <typename T>
void CustomVector<T>::push_back(const T& value) {
if (_size == _capacity) {
resize(_capacity * 2); // Double the capacity if full
}
data[_size] = value;
_size++;
}
// Remove the last element
template <typename T>
void CustomVector<T>::pop_back() {
if (_size > 0) {
_size--;
}
}
// Access element at a specific index with no bounds checking
template <typename T>
T& CustomVector<T>::operator[](size_t index) {
return data[index];
}
// Access element at a specific index with bounds checking
template <typename T>
T& CustomVector<T>::at(size_t index) {
if (index >= _size) {
throw std::out_of_range("Index out of range");
}
return data[index];
}
// Get the current size (number of elements)
template <typename T>
size_t CustomVector<T>::size() const {
return _size;
}
// Get the current capacity (allocated memory)
template <typename T>
size_t CustomVector<T>::capacity() const {
return _capacity;
}
// Resize the vector to a new capacity
template <typename T>
void CustomVector<T>::resize(size_t new_capacity) {
T* new_data = new T[new_capacity];
for (size_t i = 0; i < _size; i++) {
new_data[i] = data[i];
}
delete[] data;
data = new_data;
_capacity = new_capacity;
}
// Check if the vector is empty
template <typename T>
bool CustomVector<T>::empty() const {
return _size == 0;
}
// Clear all elements in the vector
template <typename T>
void CustomVector<T>::clear() {
_size = 0;
}
// Get the first element
template <typename T>
T& CustomVector<T>::front() {
if (_size == 0) throw std::out_of_range("Vector is empty");
return data[0];
}
// Get the last element
template <typename T>
T& CustomVector<T>::back() {
if (_size == 0) throw std::out_of_range("Vector is empty");
return data[_size - 1];
}
#endif // CUSTOMVECTOR_H