Advertisement
BeloMaximka

Belov_LW_O4

Sep 23rd, 2021
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. #pragma once
  2. #pragma once
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. template <class T>
  7. class MyVector
  8. {
  9.     T* ptr;
  10.     int size;
  11.     static int count; //колво векторов
  12.     friend ostream& operator<<<>(ostream& os, const MyVector<T>& obj);
  13.     friend istream& operator>><>(istream& is, MyVector<T>& obj);
  14. public:
  15.     MyVector(); //0
  16.     MyVector(int size);
  17.     MyVector(const MyVector& obj);
  18.     MyVector(initializer_list<T> a);
  19.     ~MyVector();
  20.     MyVector<T> operator+(const MyVector& obj);
  21.     MyVector<T> operator-(const MyVector& obj);
  22.     MyVector<T> operator*(const int mult);
  23.     MyVector<T> operator*(const MyVector& obj);
  24.     T& operator=(const T& obj);
  25.     T& operator[](int index)const;
  26.     void operator()()const;
  27.     int getSize()const;
  28.     static int getCount();
  29. };
  30.  
  31. template <class T>
  32. int MyVector<T>::count = 0;
  33.  
  34. template <class T>
  35. ostream& operator<<(ostream& os, const MyVector<T>& obj);
  36.  
  37. template <class T>
  38. istream& operator>>(istream& is, MyVector<T>& obj);
  39.  
  40. template<class T>
  41. inline MyVector<T>::MyVector()
  42. {
  43.     size = 0;
  44.     ptr = nullptr;
  45.     count++;
  46. }
  47.  
  48. template<class T>
  49. inline MyVector<T>::MyVector(int size)
  50. {
  51.     this->size = size;
  52.     ptr = new T[size];
  53.     count++;
  54. }
  55.  
  56. template<class T>
  57. inline MyVector<T>::MyVector(const MyVector& obj)
  58. {
  59.     size = obj.size;
  60.     ptr = new T[size];
  61.     for (int i = 0; i < size; i++) ptr[i] = obj.ptr[i];
  62.     count++;
  63. }
  64.  
  65. template<class T>
  66. inline MyVector<T>::MyVector(initializer_list<T> a)
  67. {
  68.     size = a.size();
  69.     ptr = new T[size];
  70.     for (auto cptr = a.begin(); cptr < a.end(); cptr++)
  71.     {
  72.         *ptr = *cptr;
  73.         ptr++;
  74.     }
  75.     ptr -= size;
  76. }
  77.  
  78. template<class T>
  79. inline MyVector<T>::~MyVector()
  80. {
  81.     delete[] ptr;
  82. }
  83.  
  84. template<class T>
  85. inline MyVector<T> MyVector<T>::operator+(const MyVector& obj)
  86. {
  87.     MyVector<T> new_obj(size + obj.size);
  88.     for (int i = 0; i < size; i++) new_obj[i] = ptr[i];
  89.     for (int i = 0; i < obj.size; i++) new_obj[size + i] = obj[i];
  90.     return new_obj;
  91. }
  92.  
  93. template<class T>
  94. inline MyVector<T> MyVector<T>::operator-(const MyVector<T>& obj)
  95. {
  96.     if (size == obj.size) return MyVector<T>();
  97.  
  98.     if (size > obj.size)
  99.     {
  100.         MyVector<T> new_obj(size - obj.size);
  101.         for (int i = 0; i < size - obj.size; i++) new_obj[i] = ptr[i];
  102.         return new_obj;
  103.     }
  104.     else
  105.     {
  106.         MyVector<T> new_obj(obj.size - size);
  107.         for (int i = 0; i < obj.size - size; i++) new_obj[i] = obj[i];
  108.         return new_obj;
  109.     }
  110. }
  111.  
  112. template<class T>
  113. inline MyVector<T> MyVector<T>::operator*(const int mult)
  114. {
  115.     if (size == 0)return MyVector<T>();
  116.     MyVector<T> new_obj(size);
  117.     for (int i = 0; i < size; i++) new_obj[i] = ptr[i] * mult;
  118.     return new_obj;
  119. }
  120.  
  121. template<class T>
  122. inline MyVector<T> MyVector<T>::operator*(const MyVector& obj)
  123. {
  124.     if (size != obj.size) return *this;
  125.  
  126.     MyVector<T> new_obj(size);
  127.     for (int i = 0; i < size; i++) new_obj[i] = ptr[i] * obj[i];
  128.  
  129.     return new_obj;
  130. }
  131.  
  132. template<class T>
  133. inline T& MyVector<T>::operator=(const T& obj)
  134. {
  135.     if (this == &obj) return *this;
  136.  
  137.     if (ptr != nullptr) delete[] ptr;
  138.     size = obj.size;
  139.  
  140.     ptr = new T[size];
  141.     for (int i = 0; i < size; i++) ptr[i] = obj.ptr[i];
  142.  
  143.     return *this;
  144. }
  145.  
  146. template<class T>
  147. inline T& MyVector<T>::operator[](int index)const
  148. {
  149.     return ptr[index];
  150. }
  151.  
  152. template<class T>
  153. inline void MyVector<T>::operator()() const
  154. {
  155.     cout << *this;
  156. }
  157.  
  158. template<class T>
  159. inline int MyVector<T>::getSize() const
  160. {
  161.     return size;
  162. }
  163.  
  164. template<class T>
  165. inline int MyVector<T>::getCount()
  166. {
  167.     return count;
  168. }
  169.  
  170. template<class T>
  171. inline ostream& operator<<(ostream& os, const MyVector<T>& obj)
  172. {
  173.     for (int i = 0; i < obj.size; i++)
  174.     {
  175.         os << obj.ptr[i] << ", ";
  176.     }
  177.     os << "\b\b ";
  178.     return os;
  179. }
  180.  
  181. template<class T>
  182. inline istream& operator>>(istream& is, MyVector<T>& obj)
  183. {
  184.     for (int i = 0; i < obj.size; i++)
  185.     {
  186.         cout << i + 1 << " element: ";
  187.         is >> obj.ptr[i];
  188.     }
  189.     return is;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement