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
|
#include "std_lib_facilities.h"
#include "Numbers.h"
#ifndef Vector_Advanced_Vectors_h
#define Vector_Advanced_Vectors_h
template <typename T>
vector<T> f(vector<T>& v1, vector<T>& v2)
{
if (v1.size() != v2.size()) error("Impossible to compute the calculation, different sizes.");
for (int i = 0; i < v1.size(); ++i)
{
v1[i] += v2[i];
}
return v1;
}
template <typename T>
void print_vec(const vector<T>& v)
{
for (int i = 0; i < v.size(); ++i)
{
cout << i + 1 << ") "
<< v[i] << endl;
}
}
template <typename T,typename U>
class mix
{
private:
vector<T> vt;
vector<U> vu;
public:
mix() : vt{0}, vu{0} {};
mix(vector<T> v1, vector<U> v2) : vt{v1}, vu{v2} {};
~mix() {};
vector<U> f_mix(vector<T>& vt, vector<U>& vu);
vector<T> f_mix_num(vector<Number<T>> vt, vector<Number<U>> vu);
vector<T> get_v1() const {return vt;}
vector<U> get_v2() const {return vu;}
double sum_vec(const vector<U>& vu);
void operator=(const mix<T,U>& n ) // Overloaded operator= (Special operator that should be as member function)
{
vt = n.vt;
vu = n.vu;
}
};
template <typename T, typename U>
vector<U> mix<T,U>::f_mix(vector<T>& vt, vector<U>& vu)
{
if (vt.size() != vu.size()) error("Impossible to compute the calculation, different sizes.");
vector<U> vu1 = vu;
for (int i = 0; i < vt.size(); ++i)
{
vu1[i] = vt[i] * vu[i];
}
return vu1;
}
template <typename T, typename U>
vector<T> mix<T,U>::f_mix_num(vector<Number<T>> vt, vector<Number<U>> vu)
{
if (vt.size() != vu.size()) error("Impossible to compute the calculation, different sizes.");
vector<Number<T>> vu1 = vt;
for (int i = 0; i < vt.size(); ++i)
{
vu1[i] = vt[i] * vu[i];
}
return vu1;
}
template <typename T, typename U>
double mix<T,U>::sum_vec(const vector<U>& vu)
{
double result = 0.0;
for (int i = 0; i < vu.size(); ++i)
{
result += vu[i];
}
return result;
}
/*
template<> // Specialization with both T and U specialized ---> Template typenames empty "<>" because
// All specialized
class mix<vector<int>,vector<double>> // Specialization
{
vector<double> f_mix(vector<int>& vt, vector<double>& vu);
vector<double> operator=(vector<int>& v1);
};
vector<double> mix<vector<int>,vector<double>>::f_mix(vector<int>& vt, vector<double>& vu)
{
if (vt.size() != vu.size()) error("Impossible to compute the calculation, different sizes.");
vector<double> vu1;
for (int i = 0; i < vt.size(); ++i)
{
double n = 0.0;
n = vt[i] * vu[i];
vu1.push_back(n);
}
return vu1;
}
*/
#endif
|