Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template<typename T>
- class Vector
- {
- private:
- T *Array;
- int Size;
- int maxSize;
- public:
- Vector()
- {
- Array = NULL;
- Size = 0;
- maxSize = 1;
- }
- ~Vector()
- {
- delete[] Array;
- }
- Vector(int n)
- {
- Size = n;
- Array = new T[n];
- maxSize = n * 2;
- }
- Vector(const Vector<T> &Construct)
- {
- Array = new T[Construct.maxSize];
- int i = 0;
- while(i < Construct.Size)
- {
- Array[i] = Construct.Array[i];
- i++;
- }
- Size = Construct.Size;
- maxSize = Construct.maxSize;
- }
- int Lenght()
- {
- return Size;
- }
- void Push_back(T Value)
- {
- if(Size == maxSize)
- {
- maxSize = maxSize * 2;
- T *cashArray = new T[maxSize];
- int i = 0;
- while(i < Size)
- {
- cashArray[i] = Array[i];
- i++;
- }
- delete[] Array;
- Array = cashArray;
- delete[] cashArray;
- }
- Array[Size] = Value;
- Size++;
- }
- void Pop_back()
- {
- if(Size > 0)
- {
- T *cashArray = new T[maxSize];
- int i = 0;
- while(i < Size-1)
- {
- cashArray[i] = Array[i];
- i++;
- }
- delete[] Array;
- Array = cashArray;
- Size--;
- }
- }
- void Reserve(int border)
- {
- T *cashArray = new T[border];
- int i = 0;
- while(i < border)
- {
- cashArray[i] = Array[i];
- i++;
- }
- delete[] Array;
- Array[i] = cashArray[i];
- Size = border;
- }
- void Resize(int FirstB, int SecondB)
- {
- if(FirstB > SecondB) swap(FirstB, SecondB);
- FirstB--;
- int newSize = SecondB - FirstB;
- if(Size - SecondB < 0) newSize = Size - FirstB;
- T *cashArray = new T[newSize];
- int i = 0;
- while(i < newSize)
- {
- cashArray[i] = Array[FirstB];
- i++;
- FirstB++;
- }
- delete[] Array;
- Array = cashArray;
- Size = newSize;
- }
- T& operator[](int index)
- {
- return Array[index];
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement