diff options
| author | Syndamia <kamen@syndamia.com> | 2024-05-25 16:08:17 +0300 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2024-05-25 16:08:17 +0300 |
| commit | 9c11fababb9b6b194e646fbeb62d141aacf74c43 (patch) | |
| tree | f1a294fcd66b22463b9424c838ff171db8539a15 /week13/Exercise2/Resizeable.hpp | |
| parent | c6fae5f1a0cc8394b1de3b671f07de7b3d677c59 (diff) | |
| download | oop-2023-solutions-9c11fababb9b6b194e646fbeb62d141aacf74c43.tar oop-2023-solutions-9c11fababb9b6b194e646fbeb62d141aacf74c43.tar.gz oop-2023-solutions-9c11fababb9b6b194e646fbeb62d141aacf74c43.zip | |
[w13] Added solutions
Diffstat (limited to 'week13/Exercise2/Resizeable.hpp')
| -rw-r--r-- | week13/Exercise2/Resizeable.hpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/week13/Exercise2/Resizeable.hpp b/week13/Exercise2/Resizeable.hpp new file mode 100644 index 0000000..9b4fe04 --- /dev/null +++ b/week13/Exercise2/Resizeable.hpp @@ -0,0 +1,34 @@ +#pragma once +#include "Container.hpp" + +template <class T> +class Resizeable : virtual public Container<T> { + void resize(); + +public: + void InsertAt(const T& element, unsigned index); +}; + +template <class T> +void Resizeable<T>::resize() { + this->size++; + T* biggerArr = new T[this->size]; + for (int i = 0; i < this->size - 1; i++) { + biggerArr[i] = this->arr[i]; + } + delete[] this->arr; + this->arr = biggerArr; +} + +template <class T> +void Resizeable<T>::InsertAt(const T& element, unsigned index) { + if (index >= this->size) { + throw "Index out of range!"; + } + + resize(); + for (int i = this->size - 1; i > index; i++) { + this->arr[i] = this->arr[i-1]; + } + this->arr[index] = element; +} |
