-
Notifications
You must be signed in to change notification settings - Fork 8
/
templates.cpp
45 lines (37 loc) · 962 Bytes
/
templates.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
#include <iostream>
using namespace std;
template <class T>
class Vector {
T *arr;
int size;
public:
// Constructor to initialize the vector with a given array and size
Vector(T* arr, int size) {
this->size = size;
this->arr = new T[size];
for (int i = 0; i < size; i++) {
this->arr[i] = arr[i];
}
}
// Destructor to clean up the allocated memory
~Vector() {
delete[] arr;
}
// Method to print the vector
void print() {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
// Additional methods can be added here
};
int main() {
int arr1[] = {1, 2, 3, 4, 5};
float arr2[] = {1.1, 2.2, 3.3, 4.4, 5.5};
Vector<int> myVec1(arr1, 5); // Creating a vector of integers
Vector<float> myVec2(arr2, 5); // Creating a vector of floats
myVec1.print();
myVec2.print();
return 0;
}