Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #pragma once
- #include <iostream>
- using namespace std;
- template <class T>
- class MyVector
- {
- T* ptr;
- int size;
- static int count; //колво векторов
- friend ostream& operator<<<>(ostream& os, const MyVector<T>& obj);
- friend istream& operator>><>(istream& is, MyVector<T>& obj);
- public:
- MyVector(); //0
- MyVector(int size);
- MyVector(const MyVector& obj);
- MyVector(initializer_list<T> a);
- ~MyVector();
- MyVector<T> operator+(const MyVector& obj);
- MyVector<T> operator-(const MyVector& obj);
- MyVector<T> operator*(const int mult);
- MyVector<T> operator*(const MyVector& obj);
- T& operator=(const T& obj);
- T& operator[](int index)const;
- void operator()()const;
- int getSize()const;
- static int getCount();
- };
- template <class T>
- int MyVector<T>::count = 0;
- template <class T>
- ostream& operator<<(ostream& os, const MyVector<T>& obj);
- template <class T>
- istream& operator>>(istream& is, MyVector<T>& obj);
- template<class T>
- inline MyVector<T>::MyVector()
- {
- size = 0;
- ptr = nullptr;
- count++;
- }
- template<class T>
- inline MyVector<T>::MyVector(int size)
- {
- this->size = size;
- ptr = new T[size];
- count++;
- }
- template<class T>
- inline MyVector<T>::MyVector(const MyVector& obj)
- {
- size = obj.size;
- ptr = new T[size];
- for (int i = 0; i < size; i++) ptr[i] = obj.ptr[i];
- count++;
- }
- template<class T>
- inline MyVector<T>::MyVector(initializer_list<T> a)
- {
- size = a.size();
- ptr = new T[size];
- for (auto cptr = a.begin(); cptr < a.end(); cptr++)
- {
- *ptr = *cptr;
- ptr++;
- }
- ptr -= size;
- }
- template<class T>
- inline MyVector<T>::~MyVector()
- {
- delete[] ptr;
- }
- template<class T>
- inline MyVector<T> MyVector<T>::operator+(const MyVector& obj)
- {
- MyVector<T> new_obj(size + obj.size);
- for (int i = 0; i < size; i++) new_obj[i] = ptr[i];
- for (int i = 0; i < obj.size; i++) new_obj[size + i] = obj[i];
- return new_obj;
- }
- template<class T>
- inline MyVector<T> MyVector<T>::operator-(const MyVector<T>& obj)
- {
- if (size == obj.size) return MyVector<T>();
- if (size > obj.size)
- {
- MyVector<T> new_obj(size - obj.size);
- for (int i = 0; i < size - obj.size; i++) new_obj[i] = ptr[i];
- return new_obj;
- }
- else
- {
- MyVector<T> new_obj(obj.size - size);
- for (int i = 0; i < obj.size - size; i++) new_obj[i] = obj[i];
- return new_obj;
- }
- }
- template<class T>
- inline MyVector<T> MyVector<T>::operator*(const int mult)
- {
- if (size == 0)return MyVector<T>();
- MyVector<T> new_obj(size);
- for (int i = 0; i < size; i++) new_obj[i] = ptr[i] * mult;
- return new_obj;
- }
- template<class T>
- inline MyVector<T> MyVector<T>::operator*(const MyVector& obj)
- {
- if (size != obj.size) return *this;
- MyVector<T> new_obj(size);
- for (int i = 0; i < size; i++) new_obj[i] = ptr[i] * obj[i];
- return new_obj;
- }
- template<class T>
- inline T& MyVector<T>::operator=(const T& obj)
- {
- if (this == &obj) return *this;
- if (ptr != nullptr) delete[] ptr;
- size = obj.size;
- ptr = new T[size];
- for (int i = 0; i < size; i++) ptr[i] = obj.ptr[i];
- return *this;
- }
- template<class T>
- inline T& MyVector<T>::operator[](int index)const
- {
- return ptr[index];
- }
- template<class T>
- inline void MyVector<T>::operator()() const
- {
- cout << *this;
- }
- template<class T>
- inline int MyVector<T>::getSize() const
- {
- return size;
- }
- template<class T>
- inline int MyVector<T>::getCount()
- {
- return count;
- }
- template<class T>
- inline ostream& operator<<(ostream& os, const MyVector<T>& obj)
- {
- for (int i = 0; i < obj.size; i++)
- {
- os << obj.ptr[i] << ", ";
- }
- os << "\b\b ";
- return os;
- }
- template<class T>
- inline istream& operator>>(istream& is, MyVector<T>& obj)
- {
- for (int i = 0; i < obj.size; i++)
- {
- cout << i + 1 << " element: ";
- is >> obj.ptr[i];
- }
- return is;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement