-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtvector.cpp
More file actions
72 lines (60 loc) · 935 Bytes
/
Copy pathtvector.cpp
File metadata and controls
72 lines (60 loc) · 935 Bytes
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
#include<iostream>
using namespace std;
template<class T>
class vector{
T* arr;
int capacity;
int size;
public:
vector(){
arr = new T[capacity=1];
size=0;
}
void push(T data){
if(size==capacity){
T* temp = new T[2*capacity];
for(int i=0;i<size;i++)
temp[i]=arr[i];
delete[] arr;
capacity*=2;
arr=temp;
}
arr[size]=data;
size++;
}
void push(int index,T data){
if(index==size){
push(data);
}else if(index>size){
cout<<"can't push beyond size!!"<<endl;
}else{
arr[index]=data;
}
}
void pop(){
size--;
}
void print(){
for(int i=0;i<size;i++){
cout<<arr[i]<<"\t";
}cout<<endl;
}
T operator*(vector &y){
T sum = 0;
for(int i=0;i<size;i++)
sum+=this->arr[i] * y.arr[i];
return sum;
}
};
int main(){
vector<int> v1;
v1.push(1);
v1.push(2);
v1.push(3);
vector<int> v2;
v2.push(4);
v2.push(5);
v2.push(6);
int R = v1*v2;
cout<<R<<endl;
}