Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- using namespace std;
- template <class T>
- class SimpleVector
- {
- private:
- T* _array;
- int _arraySize;
- public:
- void push_back(T value)
- {
- T* _result = new T[++this->_arraySize];
- for (int i = 0; i < this->_arraySize; ++i)
- {
- if (i != this->_arraySize - 1) _result[i] = this->_array[i];
- else
- {
- _result[i] = value;
- break;
- }
- }
- delete[] this->_array;
- this->_array = _result;
- }
- T pop_back()
- {
- return this->erase(this->_arraySize - 1);
- }
- void clear()
- {
- this->_arraySize = 0;
- this->_array = new T[this->_arraySize];
- }
- T operator[](int i)
- {
- return this->_array[i];
- }
- int size()
- {
- return this->_arraySize;
- }
- void insert(T value, int i)
- {
- int oldSize = this->_arraySize;
- this->_arraySize = (this->_arraySize >= i) ? (++this->_arraySize) : (this->_arraySize + (i - this->_arraySize));
- T* _result = new T[this->_arraySize];
- int arrIndex = 0;
- for (int i2 = 0; i2 < this->_arraySize; ++i2)
- {
- if (i2 == i)
- {
- _result[i2] = value;
- continue;
- }
- if (arrIndex != oldSize) _result[i2] = this->_array[arrIndex];
- }
- delete[] this->_array;
- this->_array = _result;
- }
- T erase(int i)
- {
- T* _result = new T[--this->_arraySize];
- T resItem;
- int resIndex = 0;
- for (int i2 = 0; i2 < this->_arraySize + 1; ++i2)
- {
- T item = this->_array[i2];
- if (i == i2)
- {
- resItem = item;
- continue;
- }
- _result[resIndex++] = this->_array[i2];
- }
- delete[] this->_array;
- this->_array = _result;
- return resItem;
- }
- int find(int value)
- {
- for (int i = 0; i < this->_arraySize; ++i)
- {
- if (this->_array[i] == value)
- {
- return i;
- }
- }
- return -1;
- }
- void remove(int value)
- {
- T* _result = new T[--this->_arraySize];
- int resIndex = 0, k = 0;
- for (int i = 0; i < this->_arraySize; ++i)
- {
- if (this->_array[i] == value && k == 0)
- {
- k++;
- continue;
- }
- _result[resIndex++] = this->_array[i];
- }
- delete[] this->_array;
- this->_array = _result;
- }
- bool insert_after(int x, int y)
- {
- T* _result = new T[++this->_arraySize];
- int resIndex = 0;
- if (this->find(y) == -1) return false;
- else
- {
- int yPos = this->find(y);
- for (int i = 0; i < this->_arraySize; ++i)
- {
- _result[resIndex++] = this->_array[i];
- if (i == yPos) _result[resIndex++] = x;
- }
- return true;
- }
- }
- SimpleVector()
- {
- this->_arraySize = 0;
- this->_array = new T[this->_arraySize];
- }
- ~SimpleVector()
- {
- delete[] this->_array;
- }
- };
Add Comment
Please, Sign In to add comment