Last active
December 17, 2015 20:19
-
-
Save DarTheStrange/5666695 to your computer and use it in GitHub Desktop.
Templated array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
template<class T> | |
class array { | |
int size; | |
T* g; | |
public: | |
array(int size = 8){ | |
this->size = size; | |
this->g = new T[size]; | |
} | |
array(const array<T>& a){ | |
this->size = a.size; | |
this->g = new T[this->size]; | |
for (int i = 0; this->size - i; ++i) | |
this->g[i] = a.g[i]; | |
} | |
~array(){ | |
delete[] g; | |
} | |
array<T>& operator=(const array<T>& a){ | |
this->size = a.size; | |
this->g = new T[this->size]; | |
for (int i = 0; this->size - i; ++i) | |
this->g[i] = a.g[i]; | |
return *this; | |
} | |
T& operator[] (int y){ | |
return g[y]; | |
} | |
const T& operator[] (int y) const{ | |
return g[y]; | |
} | |
void resize(int y){ | |
T* temp = new T[y]; | |
for (int i = 0; y - i && size - i; ++i){ | |
temp[i] = g[i]; | |
} | |
delete[] g; | |
g = temp; | |
size = y; | |
} | |
void append(T t){ | |
resize(size + 1); | |
(*this)[size - 1] = t; | |
} | |
}; | |
int main(){ | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment