Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
72
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 <vector>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <random>
  6. #include <algorithm>
  7.  
  8.  
  9. template<typename T>
  10. class Vector
  11. {
  12. public:
  13.     enum
  14.     {
  15.         CAPACITY = 8
  16.     };
  17.  
  18.     // конструктор
  19.     explicit Vector(std::size_t init_size = 0) : VSize(init_size), VCapacity(init_size + CAPACITY)
  20.     {
  21.         std::cout << "Ctor\n";
  22.         vec = new T[VCapacity];
  23.     }
  24.  
  25.     // конструктор копирования
  26.     Vector(const Vector &right) : vec(nullptr)
  27.     {
  28.         std::cout << "CCtor\n";
  29.         VSize = right.size();
  30.         VCapacity = right.capacity();
  31.  
  32.         vec = new T[VCapacity];
  33.  
  34.         for (int i = 0; i < VSize; ++i)
  35.             vec[i] = right.vec[i];
  36.     }
  37.  
  38.     // конструктор перемещения
  39.     Vector(Vector &&right) : vec(nullptr)
  40.     {
  41.         std::cout << "CtorMove\n";
  42.         operator=(std::move(right));
  43.     }
  44.  
  45.     // деструктор
  46.     ~Vector()
  47.     {
  48.         std::cout << "Dtor\n";
  49.         delete[] vec;
  50.     }
  51.  
  52.     const Vector &operator=(const Vector &right)
  53.     {
  54.         Vector vector(right);
  55.         *this = std::move(vector);
  56.     }
  57.  
  58.     void resize(std::size_t newSize)
  59.     {
  60.         reserve(newSize * 2 + 1);
  61.         VSize = newSize;
  62.     }
  63.  
  64.     void reserve(size_t newCapacity)
  65.     {
  66.         if (newCapacity > VCapacity)
  67.         {
  68.             T* tempVec = new T[newCapacity];
  69.  
  70.             for (int i = 0; i < VSize; i++)
  71.                 tempVec[i] = vec[i];
  72.  
  73.             delete[] vec;
  74.             vec = new T[newCapacity];
  75.             vec = tempVec;
  76.         }
  77.  
  78.  
  79.         if (newCapacity < VCapacity)
  80.         {
  81.             for (int i =  VCapacity; i != newCapacity; --i)
  82.                 vec[i] = NULL;
  83.         }
  84.  
  85.         VCapacity = newCapacity;
  86.     }
  87.  
  88.     T &operator[](int index)
  89.     {
  90.         return vec[index];
  91.     }
  92.  
  93.     typedef T* viterator;
  94.  
  95.     viterator begin() const
  96.     {
  97.         return vec;
  98.     }
  99.  
  100.     viterator end() const
  101.     {
  102.         return vec + VSize;
  103.     }
  104.  
  105.  
  106.     const bool empty() const
  107.     {
  108.         return VSize == 0;
  109.     }
  110.  
  111.     void push_back(T value)
  112.     {
  113.         resize(VSize + 1);
  114.         vec[VSize - 1] = value;
  115.     }
  116.  
  117.     const size_t size() const
  118.     {
  119.         return VSize;
  120.     }
  121.  
  122.     const size_t capacity() const
  123.     {
  124.         return VCapacity;
  125.     }
  126.     void pop_back()
  127.     {
  128.         if (!empty())
  129.         {
  130.             vec[VSize - 1] = NULL;
  131.             --VSize;
  132.         }
  133.     }
  134.  
  135.     const T back()
  136.     {
  137.         if (!empty())
  138.             return vec[VSize - 1];
  139.     }
  140.  
  141.     const T front()
  142.     {
  143.         if (!empty())
  144.             return vec[0];
  145.     }
  146.  
  147.     template<typename T1>
  148.     friend std::ostream &operator<<(std::ostream &out, const Vector<T1> &obj);
  149.  
  150. private:
  151.     std::size_t VSize;
  152.     std::size_t VCapacity;
  153.     T* vec;
  154. };
  155.  
  156. template<typename T1>
  157. std::ostream &operator<<(std::ostream &out, const Vector<T1> &obj)
  158. {
  159.     // вывод по 10 элементов в одной строке (переменная num).
  160.     int num = 0;
  161.     for (auto &i : obj)
  162.     {
  163.         out << i << " ";
  164.         ++num;
  165.         if (num / 11)
  166.         {
  167.             std::cout << std::endl;
  168.             num = 0;
  169.         }
  170.     }
  171.     std::cout << std::endl;
  172.     return out;
  173. }
  174.  
  175. int random_generate(int min, int max)
  176. {
  177.     std::random_device rd;     // only used once to initialise (seed) engine
  178.     std::mt19937 rng(rd());    // random-number engine used (Mersenne-Twister in this case)
  179.     std::uniform_int_distribution<int> uni(min,max); // guaranteed unbiased
  180.  
  181.     return uni(rng);
  182. }
  183.  
  184. int main()
  185. {
  186.  
  187.     Vector<int> vec_one(5);
  188.  
  189.     vec_one[0] = 11;
  190.     vec_one[1] = 17;
  191.     vec_one[2] = 2;
  192.     vec_one[3] = 3;
  193.     vec_one[4] = 14;
  194.     std::cout << vec_one;
  195.  
  196.     vec_one.push_back(1);
  197.     std::cout << vec_one;
  198.  
  199.     vec_one.pop_back();
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement