Advertisement
RobertDeMilo

BB2.14 Ссылка на себя

Jun 9th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. template <typename T>
  6. class SimpleVector
  7. {
  8. public:
  9.     SimpleVector() = default;
  10.     explicit SimpleVector(size_t size);
  11.     SimpleVector(const SimpleVector<T>& other);
  12.     SimpleVector(SimpleVector&& other);
  13.     SimpleVector<T>& operator=(const SimpleVector& other);
  14.     SimpleVector<T>& operator=(SimpleVector&& other);
  15.     ~SimpleVector();
  16.     T& operator[](size_t index);
  17.     T* begin();
  18.     T* end();
  19.     const T* begin() const;
  20.     const T* end() const;
  21.     size_t Size() const;
  22.     size_t Capacity() const;
  23.     void PushBack(const T& value);
  24.     void PushBack(T&& value);
  25.    
  26. private:
  27.     T* data = nullptr;
  28.     size_t size = 0;
  29.     size_t capacity = 0;
  30. };
  31.  
  32. template <typename T>
  33. inline SimpleVector<T>::SimpleVector(size_t size)
  34.     : data(new T[size]), size(size), capacity(size) { cout << this << endl; }
  35.  
  36. template <typename T>
  37. SimpleVector<T>::SimpleVector(const SimpleVector<T>& other)
  38.     : data(new T[other.capacity]), size(other.size), capacity(other.capacity)
  39. {
  40.     copy(other.begin(), other.end(), begin());
  41. }
  42.  
  43. template <typename T>
  44. SimpleVector<T>::SimpleVector(SimpleVector<T>&& other)
  45.     : data(other.data), size(other.size), capacity(other.capacity)
  46. {
  47.     other.data = nullptr;
  48.     other.size = other.capacity = 0;
  49. }
  50.  
  51. template <typename T>
  52. SimpleVector<T>& SimpleVector<T>::operator=(const SimpleVector<T>& other)
  53. {
  54.       if(this!=&other)
  55.       {
  56.         delete[] data;
  57.         data = new T[other.capacity];
  58.         size = other.size;
  59.         capacity = other.capacity;
  60.         copy(other.begin(), other.end(), begin());
  61.       }
  62.    
  63.       return *this;
  64. }
  65.  
  66. template <typename T>
  67. SimpleVector<T>& SimpleVector<T>::operator=(SimpleVector<T>&& other)
  68. {
  69.     if(this!=&other)
  70.     {
  71.         delete[] data;
  72.         data = other.data;
  73.         size = other.size;
  74.         capacity = other.capacity;
  75.         other.data = nullptr;
  76.         other.size = other.capacity = 0;    
  77.     }
  78.    
  79.     return *this;
  80. }
  81.  
  82. template <typename T>
  83. void SimpleVector<T>::PushBack(const T& value)
  84. {
  85.     if (size >= capacity)
  86.     {
  87.         auto new_cap = (capacity == 0) ? 1 : 2 * capacity;
  88.         auto new_data = new T[new_cap];
  89.         copy(begin(), end(), new_data);
  90.  
  91.         delete[] data;
  92.         data = new_data;
  93.         capacity = new_cap;
  94.     }
  95.     data[size++] = value;
  96. }
  97.  
  98.  
  99. template <typename T>
  100. inline size_t SimpleVector<T>::Capacity()const
  101. {
  102.     return capacity;
  103. }
  104.  
  105. template <typename T>
  106. inline SimpleVector<T>::~SimpleVector()
  107. {
  108.     delete[] data;
  109. }
  110.  
  111. template <typename T>
  112. inline T& SimpleVector<T>::operator[](size_t index)
  113. {
  114.     return data[index];
  115. }
  116.  
  117. template<typename T>
  118. T* SimpleVector<T>::begin()
  119. {
  120.     return data;
  121. }
  122.  
  123. template<typename T>
  124. T* SimpleVector<T>::end()
  125. {
  126.     return data + size;
  127. }
  128.  
  129. template<typename T>
  130. const T* SimpleVector<T>::begin() const
  131. {
  132.     return data;
  133. }
  134.  
  135. template<typename T>
  136. const T* SimpleVector<T>::end() const
  137. {
  138.     return data + size;
  139. }
  140.  
  141. template <typename T>
  142. inline size_t SimpleVector<T>::Size() const
  143. {
  144.     return size;
  145. }
  146.  
  147. template <typename T>
  148. ostream & operator << (ostream& os, SimpleVector<T>& rhs)
  149. {
  150.     os<<"Size = "<<rhs.Size()<<" Items:";
  151.    
  152.     for(const auto & x:rhs)
  153.     {
  154.         os<<' '<<x;
  155.     }
  156.     return os;
  157. }
  158.  
  159. int main()
  160. {
  161.    
  162.   //int a,b,c,d,e;
  163.   //a = (b = (c = (d = (e = 0))));
  164.  
  165.   SimpleVector<int> e(5);
  166.   SimpleVector<int> a, b, c, d;
  167.   a = b = c = d = e;
  168.   cout << a.Size() << endl;
  169.    
  170.     // Оператор присваивания правоассоциативен
  171.     // Чтобы работало цепное присваивание, оператор присваивания должен возвращать ссылку на себя
  172.     // Это делается с помощью возврата *this
  173.    
  174.     return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement