TrickmanOff

taskkkk

Sep 30th, 2020
667
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. // +=
  5. // +
  6.  
  7. template <typename T>
  8. class MathVector {
  9. private:
  10.     std::vector<T> data;
  11.  
  12. public:
  13.     // Zero vector of size n
  14.     MathVector(size_t n) {
  15.         data.resize(n);
  16.     }
  17.  
  18.     template <typename Iter>
  19.     MathVector(Iter first, Iter last) {
  20.         data = std::vector<T>(first, last);
  21.     }
  22.  
  23.     size_t Dimension() const {
  24.         return data.size();
  25.     }
  26.  
  27.     T& operator [] (size_t i) {
  28.         return data[i];
  29.     }
  30.  
  31.     const T& operator [] (size_t i) const {
  32.         return data[i];
  33.     }
  34. };
  35.  
  36.  
  37. // Output format: (1, 2, 3, 4, 5)
  38. template <typename T>
  39. std::ostream& operator << (std::ostream& out, const MathVector<T>& v) {
  40.     out << '(';
  41.     for (size_t i = 0; i != v.Dimension(); ++i) {
  42.         if (i > 0)
  43.             out << ", ";
  44.         out << v[i];
  45.     }
  46.     out << ')';
  47.     return out;
  48. }
  49.  
  50. template <typename T>
  51. MathVector<T>& operator *= (MathVector<T>& v, const T& scalar) {
  52.     for (size_t i = 0; i != v.Dimension(); ++i)
  53.         v[i] *= scalar;
  54.     return v;
  55. }
  56.  
  57. template <typename T>
  58. MathVector<T> operator * (const MathVector<T>& v, const T& scalar) {
  59.     auto tmp(v);
  60.     tmp *= scalar;
  61.     return tmp;
  62. }
  63.  
  64. template <typename T>
  65. MathVector<T> operator * (const T& scalar, const MathVector<T>& v) {
  66.     return v * scalar;
  67. }
  68.  
  69. #include <set>
  70. using namespace std;
  71.  
  72. int main() {
  73.     vector<int> vec1 = {1, 2, 3};
  74.     MathVector<int> vec(vec1.begin(), vec1.end());
  75.  
  76.     int tt = 0;
  77.  
  78. }
RAW Paste Data