Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1. // хэдер для задания
  2. #ifndef HSE_VECTOR_HPP
  3. #define HSE_VECTOR_HPP
  4. #include<ostream>
  5. #include<iostream>
  6. // ... другие include директивы ...
  7.  
  8. // рассматривается упрощенный шаблон класса Vector< >
  9. // как "обёртка" вокруг динамического массива произвольной
  10. // длины
  11. template<typename T>
  12. class Vector
  13. {
  14. public:
  15.     enum { CAPACITY = 8 };
  16.  
  17.     explicit Vector(std::size_t init_size = 0) : VSize(init_size), VCapacity(init_size + CAPACITY)
  18.     {
  19.         std::cout << "Ctor\n";
  20.         vec = new T[VCapacity];
  21.     }
  22.  
  23.     // конструктор копирования
  24.     Vector(const Vector& right) : vec(nullptr)
  25.     {
  26.         std::cout << "CCtor\n";
  27.         VSize = right.size();
  28.         VCapacity = right.capacity();
  29.  
  30.         vec = new T[VCapacity];
  31.  
  32.         std::copy(right.vec, right.vec + right.VSize, vec);
  33.        /* for (int i = 0; i < VSize; ++i)
  34.             vec[i] = right.vec[i];*/
  35.     }
  36.  
  37.     // конструктор перемещения
  38.     Vector(Vector&& right) noexcept : vec(nullptr)
  39.     {
  40.         std::cout << "CtorMove\n";
  41.  
  42.         operator=(std::move(right));
  43.     }
  44.  
  45.     ~Vector()
  46.     {
  47.         std::cout << "Dtor\n";
  48.         delete[] vec;
  49.     }
  50.  
  51.     Vector& operator=(const Vector& right)
  52.     {
  53.         Vector tmp(right);
  54.         *this = std::move(tmp);
  55.  
  56.         return *this;
  57.     }
  58.  
  59.     Vector& operator=(Vector&& right) noexcept {
  60.         delete[] vec;
  61.  
  62.         vec = right.vec;
  63.         VSize = right.VSize;
  64.         VCapacity = right.VCapacity;
  65.  
  66.  
  67.         right.vec = nullptr;
  68.         right.VSize = 0;
  69.         right.VCapacity = 8;
  70.         return *this;
  71.     }
  72.  
  73.     void resize(std::size_t newSize)
  74.     {
  75.         if (newSize > VCapacity)
  76.             reserve(newSize * 2 + 1);
  77.  
  78.         VSize = newSize;
  79.     }
  80.  
  81.     void reserve(size_t newCapacity)
  82.     {
  83.         if (newCapacity > VCapacity)
  84.         {
  85.             T* tmp = new T[newCapacity];
  86.             std::copy(vec, vec + VSize, tmp);
  87.  
  88.             delete vec;
  89.             vec = tmp;
  90.             VCapacity = newCapacity;
  91.         }
  92.     }
  93.  
  94.     T& operator[](int index)
  95.     {
  96.         if (index < 0 || index >= VCapacity)
  97.             throw std::invalid_argument("Error: index is out of bounds.");
  98.  
  99.         return vec[index];
  100.     }
  101.  
  102.     const T& operator[](int index) const
  103.     {
  104.         return vec[index];
  105.     }
  106.  
  107.     bool empty() const
  108.     {
  109.         return VSize == 0;
  110.     }
  111.  
  112.     std::size_t size() const
  113.     {
  114.         return VSize;
  115.     }
  116.  
  117.     std::size_t capacity() const
  118.     {
  119.         return VCapacity;
  120.     }
  121.  
  122.     T& front()
  123.     {
  124.         return vec[0];
  125.     }
  126.  
  127.     const T& front() const
  128.     {
  129.         return vec[0];
  130.     }
  131.  
  132.     T& back()
  133.     {
  134.         return vec[VSize - 1];
  135.     }
  136.  
  137.     const T &back() const
  138.     {
  139.         return vec[0];
  140.     }
  141.  
  142.     void push_back(const T &val)
  143.     {
  144.         if (VSize == VCapacity)
  145.             reserve(VSize * 2 + 1);
  146.  
  147.         vec[VSize] = val;
  148.         VSize += 1;
  149.     };
  150.  
  151.     void push_back(T&& val)
  152.     {
  153.         if (VSize == VCapacity)
  154.             reserve(VSize * 2 + 1);
  155.  
  156.         vec[VSize] = val;
  157.         VSize += 1;
  158.         val = nullptr;
  159.     };
  160.  
  161.     void pop_back()
  162.     {
  163.         if (!empty())
  164.             VSize -= 1;
  165.     };
  166.  
  167.     // ... ОСТАВШАЯСЯ ЧАСТЬ - см. текст задания ...
  168.  
  169.     typedef T* viterator;
  170.     viterator begin() const
  171.     {
  172.         return vec;
  173.     }
  174.     viterator end() const
  175.     {
  176.         return vec + VSize;
  177.     }
  178.  
  179.     template<typename T1>
  180.     friend std::ostream& operator<<(std::ostream& out, const Vector<T1>& obj);
  181.  
  182. private:
  183.     std::size_t VSize;
  184.     std::size_t VCapacity;
  185.     T* vec;
  186. };
  187.  
  188. template<typename T1>
  189. std::ostream& operator<<(std::ostream& out, const Vector<T1>& obj)
  190. {
  191.     // вывод по 10 элементов в одной строке
  192.     int num = 0;
  193.     for (auto& i : obj)
  194.     {
  195.         out << i << " ";
  196.         ++num;
  197.         if (num / 11)
  198.         {
  199.             std::cout << std::endl;
  200.             num = 0;
  201.         }
  202.     }
  203.     std::cout << std::endl;
  204.     return out;
  205. }
  206.  
  207. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement