Advertisement
RobertDeMilo

BB2.12-13 Присваивание объекта самому себе и знакомство с this

Jun 9th, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.59 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.     void operator=(const SimpleVector& other);
  14.     void 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. void 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.  
  64. template <typename T>
  65. void SimpleVector<T>::operator=(SimpleVector<T>&& other)
  66. {
  67.     if(this!=&other)
  68.     {
  69.         delete[] data;
  70.         data = other.data;
  71.         size = other.size;
  72.         capacity = other.capacity;
  73.         other.data = nullptr;
  74.         other.size = other.capacity = 0;    
  75.     }
  76. }
  77.  
  78. template <typename T>
  79. void SimpleVector<T>::PushBack(const T& value)
  80. {
  81.     if (size >= capacity)
  82.     {
  83.         auto new_cap = (capacity == 0) ? 1 : 2 * capacity;
  84.         auto new_data = new T[new_cap];
  85.         copy(begin(), end(), new_data);
  86.  
  87.         delete[] data;
  88.         data = new_data;
  89.         capacity = new_cap;
  90.     }
  91.     data[size++] = value;
  92. }
  93.  
  94.  
  95. template <typename T>
  96. inline size_t SimpleVector<T>::Capacity()const
  97. {
  98.     return capacity;
  99. }
  100.  
  101. template <typename T>
  102. inline SimpleVector<T>::~SimpleVector()
  103. {
  104.     delete[] data;
  105. }
  106.  
  107. template <typename T>
  108. inline T& SimpleVector<T>::operator[](size_t index)
  109. {
  110.     return data[index];
  111. }
  112.  
  113. template<typename T>
  114. T* SimpleVector<T>::begin()
  115. {
  116.     return data;
  117. }
  118.  
  119. template<typename T>
  120. T* SimpleVector<T>::end()
  121. {
  122.     return data + size;
  123. }
  124.  
  125. template<typename T>
  126. const T* SimpleVector<T>::begin() const
  127. {
  128.     return data;
  129. }
  130.  
  131. template<typename T>
  132. const T* SimpleVector<T>::end() const
  133. {
  134.     return data + size;
  135. }
  136.  
  137. template <typename T>
  138. inline size_t SimpleVector<T>::Size() const
  139. {
  140.     return size;
  141. }
  142.  
  143. template <typename T>
  144. ostream & operator << (ostream& os, SimpleVector<T>& rhs)
  145. {
  146.     os << "Size = " << rhs.Size() << " Items:";
  147.    
  148.     for(const auto & x:rhs)
  149.     {
  150.         os << ' ' << x;
  151.     }
  152.    
  153.     return os;
  154. }
  155.  
  156. int main()
  157. {
  158.     SimpleVector<int> source (5);
  159.     SimpleVector<int> source2 (5);
  160.    
  161.     cout << &source << ' ' << &source2 << endl;
  162.    
  163.     for(size_t i=0; i < source.Size(); ++i)
  164.     {
  165.         source[i]=i;
  166.     }
  167.    
  168.     cout<<source<<endl;
  169.    
  170.     source = source;
  171.    
  172.     cout<<source<<endl;
  173.    
  174.    
  175.     return 0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement