awsmpshk

Класс SimpleVector, Кузичкин 141 группа

May 20th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #include <algorithm>
  2.  
  3. using namespace std;
  4.  
  5. template <class T>
  6. class SimpleVector
  7. {
  8. private:
  9.   T* _array;
  10.   int _arraySize;
  11. public:
  12.   void push_back(T value)
  13.   {
  14.     T* _result = new T[++this->_arraySize];
  15.     for (int i = 0; i < this->_arraySize; ++i)
  16.     {
  17.       if (i != this->_arraySize - 1) _result[i] = this->_array[i];
  18.       else
  19.       {
  20.         _result[i] = value;
  21.         break;
  22.       }
  23.     }
  24.     delete[] this->_array;
  25.     this->_array = _result;
  26.   }
  27.  
  28.   T pop_back()
  29.   {
  30.     return this->erase(this->_arraySize - 1);
  31.   }
  32.  
  33.   void clear()
  34.   {
  35.     this->_arraySize = 0;
  36.     this->_array = new T[this->_arraySize];
  37.   }
  38.  
  39.   T operator[](int i)
  40.   {
  41.     return this->_array[i];
  42.   }
  43.  
  44.   int size()
  45.   {
  46.     return this->_arraySize;
  47.   }
  48.  
  49.   void insert(T value, int i)
  50.   {
  51.     int oldSize = this->_arraySize;
  52.     this->_arraySize = (this->_arraySize >= i) ? (++this->_arraySize) : (this->_arraySize + (i - this->_arraySize));
  53.     T* _result = new T[this->_arraySize];
  54.     int arrIndex = 0;
  55.     for (int i2 = 0; i2 < this->_arraySize; ++i2)
  56.     {
  57.       if (i2 == i)
  58.       {
  59.         _result[i2] = value;
  60.         continue;
  61.       }
  62.  
  63.       if (arrIndex != oldSize) _result[i2] = this->_array[arrIndex];
  64.     }
  65.     delete[] this->_array;
  66.     this->_array = _result;
  67.   }
  68.  
  69.   T erase(int i)
  70.   {
  71.     T* _result = new T[--this->_arraySize];
  72.     T resItem;
  73.     int resIndex = 0;
  74.     for (int i2 = 0; i2 < this->_arraySize + 1; ++i2)
  75.     {
  76.       T item = this->_array[i2];
  77.       if (i == i2)
  78.       {
  79.         resItem = item;
  80.         continue;
  81.       }
  82.       _result[resIndex++] = this->_array[i2];
  83.     }
  84.     delete[] this->_array;
  85.     this->_array = _result;
  86.     return resItem;
  87.   }
  88.  
  89.   int find(int value)
  90.   {
  91.     for (int i = 0; i < this->_arraySize; ++i)
  92.     {
  93.       if (this->_array[i] == value)
  94.       {
  95.         return i;
  96.       }
  97.     }
  98.     return -1;
  99.   }
  100.  
  101.   void remove(int value)
  102.   {
  103.     T* _result = new T[--this->_arraySize];
  104.     int resIndex = 0, k = 0;
  105.     for (int i = 0; i < this->_arraySize; ++i)
  106.     {
  107.       if (this->_array[i] == value && k == 0)
  108.       {
  109.         k++;
  110.         continue;
  111.       }
  112.       _result[resIndex++] = this->_array[i];
  113.     }
  114.     delete[] this->_array;
  115.     this->_array = _result;
  116.   }
  117.  
  118.   bool insert_after(int x, int y)
  119.   {
  120.     T* _result = new T[++this->_arraySize];
  121.     int resIndex = 0;
  122.     if (this->find(y) == -1) return false;
  123.     else
  124.     {
  125.       int yPos = this->find(y);
  126.       for (int i = 0; i < this->_arraySize; ++i)
  127.       {
  128.         _result[resIndex++] = this->_array[i];
  129.         if (i == yPos) _result[resIndex++] = x;
  130.       }
  131.       return true;
  132.     }
  133.   }
  134.  
  135.   SimpleVector()
  136.   {
  137.     this->_arraySize = 0;
  138.     this->_array = new T[this->_arraySize];
  139.   }
  140.  
  141.   ~SimpleVector()
  142.   {
  143.     delete[] this->_array;
  144.   }
  145. };
Add Comment
Please, Sign In to add comment