Advertisement
Solingen

array.h

Dec 21st, 2024
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #ifndef ARRAY_H
  2. #define ARRAY_H
  3. #include <iostream>
  4. #include <stdexcept>
  5. using std::cout;
  6. using std::cin;
  7. using std::endl;
  8. using std::ostream;
  9.  
  10. template <typename T>
  11. class Array
  12. {
  13. private:
  14.     T* data;
  15.     int length;
  16.     int capacity;
  17. public:
  18.     void resizeArray()
  19.     {
  20.         T* newArray = new T [capacity * 2];
  21.         for (int i = 0; i < length; i++)
  22.         {
  23.             newArray[i] = data[i];
  24.         }
  25.         delete[] data;
  26.         data = newArray;
  27.         capacity *= 2;
  28.     }
  29.     Array() : length(0), capacity(1)
  30.     {
  31.         data = new T[1];
  32.     }
  33.     Array(const Array& other)
  34.     {
  35.         length = other.length;
  36.         capacity = other.capacity;
  37.         data = new T[other.capacity];
  38.         for (int i = 0; i < length; i++)
  39.         {
  40.             data[i] = other.data[i];
  41.         }
  42.  
  43.     }
  44.     Array(T arr[], int size)
  45.     {
  46.         data = new T[size];
  47.         length = size;
  48.         capacity = size;
  49.         for (int i = 0; i < size; i++)
  50.         {
  51.             data[i] = arr[i];
  52.         }
  53.        
  54.     }
  55.     Array(T a, T b)
  56.     {
  57.         data = new T[2];
  58.         data[0] = a;
  59.         data[1] = b;
  60.         length = 2, capacity = 2;
  61.     }
  62.  
  63.     T& operator[](int index) const
  64.     {
  65.         if (abs(index) > length)
  66.             throw std::overflow_error("Index out of range exception");
  67.         if (index < 0)
  68.         {
  69.             return data[length + index];
  70.         }
  71.         return data[index];
  72.     }
  73.  
  74.     int size() const
  75.     {
  76.         return length;
  77.     }
  78.  
  79.     void append(T item)
  80.     {
  81.         if (length >= capacity)
  82.         { resizeArray(); }
  83.         data[length] = item;
  84.         length++;
  85.     }
  86.  
  87.     Array<T>& operator=(const Array<T>& other)
  88.     {
  89.         if (this != &other)
  90.         {
  91.             delete[] data;
  92.             length = other.size();
  93.             capacity = other.capacity;
  94.             data = new T[capacity];
  95.             for (int i = 0; i < length; i++)
  96.             {
  97.                 data[i] = other.data[i];
  98.             }
  99.         }
  100.         return *this;
  101.     }
  102.     friend ostream& operator<<(ostream& out, const Array<T>& arr)
  103.     {
  104.         out << "{";
  105.         for (int i = 0; i < arr.size(); i++)
  106.         {
  107.             out << " ";
  108.             out << arr.data[i];
  109.         }
  110.         out << "}";
  111.         return out;
  112.     }
  113.     ~Array()
  114.     {
  115.         delete[] data;
  116.     }
  117. };
  118. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement