Created
November 19, 2014 21:52
-
-
Save webmonch/98bb958f4630d4cfa289 to your computer and use it in GitHub Desktop.
wrappedvec
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
template<class T> | |
class WrapperVector { | |
public: | |
WrapperVector(size_t n = 10): | |
_data(new T[n]()), | |
_size(n) | |
{ | |
} | |
WrapperVector(const WrapperVector<T> & other): | |
_data(new T[other.size()]), | |
_size(other.size()) | |
{ | |
for (size_t i = 0; i < _size; ++i) { | |
_data[i] = other.at(i); | |
} | |
} | |
~WrapperVector() | |
{ | |
delete [] _data; | |
} | |
WrapperVector & operator=(const WrapperVector<T> & other) | |
{ | |
delete[] _data; | |
_data = new T[other.size()]; | |
_size = other.size(); | |
for (size_t i = 0; i < _size; ++i) { | |
_data[i] = other.at(i); | |
} | |
return *this; | |
} | |
size_t size() const | |
{ | |
return _size; | |
} | |
const T & at(size_t pos) const | |
{ | |
return _data[pos]; | |
} | |
T & at(size_t pos) | |
{ | |
return _data[pos]; | |
} | |
const WrapperVector<T> operator+(WrapperVector<T> & v) | |
{ | |
WrapperVector<T> res(_size + v.size()); | |
for (size_t i = 0; i < _size; ++i) { | |
res.at(i) = _data[i]; | |
} | |
for (size_t i = 0; i < v.size(); ++i) { | |
res.at(i + _size) = v.at(i); | |
} | |
return res; | |
} | |
private: | |
T * _data; | |
size_t _size; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment