-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorAddition.cpp
More file actions
102 lines (86 loc) · 2.93 KB
/
VectorAddition.cpp
File metadata and controls
102 lines (86 loc) · 2.93 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
// Effective vector addition without temporal vectors.
#include <iostream>
#include <iomanip>
#include <string>
#include <stdlib.h>
using namespace std;
// Helper class for the sums of vectors
template<class T, long N, class Left, class Right>
class VectorSum;
// Definition of own vector class
template<class T, N>
class Vector {
T data[N]; // array of size N for storing elements of type T
public:
// operator=, whose right hand side is not a Vector, but a VectorSum representing the sum of vectors,
// for which operator[] is implemented. Enables expressions like v3 = v1 + v2
template<class Left, class Right>
Vector<T,N>& operator=(const VectorSum<T,N,Left,Right>& v) {
for (long i = 0; i < N; ++i)
data[i] = v[i];
return *this;
}
T operator[](long i) const {return data[i];}
T& operator[](long i) {return data[i];}
};
// Helper class, which stores only references to left and right hand side operands
template<class T, long N, class Left, class Right>
class VectorSum {
const Left &left;
const Right &right;
public:
VectorSum(const Left &l, const Right &r) : left(l), right(r) {}
// operator[], which returns the sum of the i-th elements of the left and right hand side operands
T operator[](long i) const { return left[i] + right[i]; }
};
// operator+ only creates a VectorSum object
// inline function, which expects two Vector type object references, and returns VectorSum object,
// which only stores the references, and the addition will happen only in operator[]
// -std=c++1y
template<class T, long N>
inline auto operator+(const Vector<T,N> &l, const Vector<T,N> &r) {
return VectorSum<T,N,Vector<T,N>,Vector<T,N>>(l,r);
}
// operator+ only creates a VectorSum object
// inline function, which expects a VectorSum and Vector type object reference, and returns VectorSum object,
// which only stores the references, and the addition will happen only in operator[]
// -std=c++1y
template<class T, long N, class Left, class Right>
inline auto operator+(const VectorSum<T,N,Left,Right> &l, const Vector<T,N> &r) {
return VectorSum<T,N,VectorSum<T,N,Left,Right>,Vector<T,N>>(l,r);
}
// function for initializing a vector with random numbers
template<class T, long N>
void init(Vector<T,N>& v) {
for (long i = 0; i < N; ++i)
v[i] = rand() % 100;
}
template<class T, long N>
void print(string s, Vector<T,N>& v) {
cout << s;
for (long i = 0; i < N; ++i)
cout << setw(4) << v[i];
cout << endl;
}
int main() {
Vector<int,5> v1;
init(v1);
print("v1 = ", v1);
Vector<int,5> v2;
init(v2);
print("v2 = ", v2);
Vector<int,5> v3;
v3 = v1 + v2;
print("v1 + v2 = ", v3);
v3 = v1 + v1 + v2;
print("v1 + v1 + v2 = ", v3);
v3 = (v1 + v1) + v2;
print("(v1 + v1) + v2 = ", v3);
return 0;
/*
v3 = v1 + (v1 + v2);
print("v1 + (v1 + v2) = ", v3);
v3 = (v1 + v2) + (v1 + v2);
print("(v1 + v2) + (v1 + v2) = ", v3);
*/
}