-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyVector.cpp
More file actions
164 lines (134 loc) · 4.51 KB
/
MyVector.cpp
File metadata and controls
164 lines (134 loc) · 4.51 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
/* my implementation of vector
Implement vector (similar to std::vector). It should grow dynamically as needed. It should store int (or use templates) and implement:
a. push_back operation
b. copy/move constructors. Copy assigment operator.
c. make it work with range loop/algorithms. How to sort it from greatest to smallest using std::sort
*/
#include <algorithm>
#include <cstring>
#include <iostream>
template <typename T>
class MyVector{
public:
// default constructor
MyVector(size_t capacity=2) :
capacity_(capacity) {
std::cout<<"Constructor called"<<std::endl;
vec_ = new T[capacity_];
}
// destructor
~MyVector() {
std::cout<<"Destructor called"<<std::endl;
delete[] vec_;
}
// copy constructor (deep copy)
MyVector(const MyVector& other_vec) :
size_(other_vec.size_),
capacity_(other_vec.capacity_) {
std::cout<<"Copy Constructor called"<<std::endl;
vec_ = new T[size_];
memcpy(vec_, other_vec.vec_, sizeof(T)*size_);
}
//move constructor
MyVector(MyVector&& other_vec) noexcept :
size_(other_vec.size_),
capacity_(other_vec.capacity_) {
std::cout<<"Move Constructor called"<<std::endl;
vec_ = other_vec.vec_;
other_vec.size_ = 0;
other_vec.capacity_ = 0;
other_vec.vec_ = nullptr;
}
// copy assignment
MyVector& operator= (const MyVector& other_vec) {
std::cout<<"Copy Assignment called"<<std::endl;
// detect self assignment
if (&other_vec != this) {
delete[] vec_;
size_ = other_vec.size_;
capacity_ = other_vec.capacity_;
vec_ = new T[size_];
memcpy(vec_, other_vec.vec_, sizeof(T)*size_);
}
return *this;
}
//move assignment
MyVector& operator= (MyVector&& other_vec) noexcept {
std::cout<<"Move Assignment called"<<std::endl;
// detect self assignment
if (&other_vec != this) {
delete[] vec_;
size_ = other_vec.size_;
capacity_ = other_vec.capacity_;
vec_ = other_vec.vec_;
other_vec.size_ = 0;
other_vec.capacity_ = 0;
other_vec.vec_ = nullptr;
}
return *this;
}
void push_back(const T& val) noexcept {
if (size_ == capacity_) {
resize();
}
vec_[size_] = val;
size_++;
}
constexpr size_t size() const noexcept {
return size_;
}
constexpr size_t capacity() const noexcept {
return capacity_;
}
// for accessing MyVector elements:
T& operator[] (size_t index) { return vec_[index]; };
const T& operator[] (size_t index) const { return vec_[index]; };
// used for for range loops:
constexpr T* begin() const noexcept {
return vec_;
}
constexpr T* end() const noexcept {
return vec_ + size();
}
private:
size_t size_{0};
size_t capacity_{0};
T* vec_{nullptr};
void resize() {
capacity_*=2;
T* temp_vec = new T[capacity_];
memcpy(temp_vec, vec_, sizeof(T)*size_);
delete[] vec_;
vec_ = temp_vec;
}
friend std::ostream& operator<< (std::ostream& os, const MyVector& my_vec) {
for (auto const& element : my_vec) {
os << element << " ";
}
return os;
}
};
int main() {
MyVector<int> vector1;
vector1.push_back(1);
vector1.push_back(2);
vector1.push_back(3);
vector1.push_back(4);
std::cout<< vector1 << std::endl;
vector1[2] = 8;
std::cout<< vector1 << std::endl;
MyVector<int> vector2(vector1);
MyVector<int> vector3;
vector2 = vector1;
vector3.push_back(9);
vector3.push_back(6);
vector3.push_back(5);
vector3.push_back(7);
vector2 = std::move(vector3);
MyVector<int> vector4(std::move(vector2));
std::cout << vector4 << std::endl;
// sort from greatest to smallest
std::sort(vector4.begin(), vector4.end(), std::greater<int>());
std::cout << vector4 << std::endl;
return 0;
}