template <typename T>
struct vector {
T* _data;
unsigned _size;
void resize (unsigned size) {
delete[] _data;
_data = new T[size];
_size = size;
}
vector() : _data(0),_size(0) {}
vector (unsigned size) : _data(0) {
resize (size);
}
vector (const vector& v) : _data(0) {
resize (v.size());
*this = v;
}
vector& operator= (const vector& v) {
if (_size!=v._size)
resize (v._size);
for (unsigned i=0;i<_size;++i)
_data[i] = v[i];
return *this;
}
unsigned size() const { return _size; }
T at (unsigned i) const { return _data[i]; }
void set (unsigned i, const T& val) {
_data[i] = val;
}
const T& operator[] (unsigned i) const { return _data[i]; }
T& operator[] (unsigned i) { return _data[i]; }
~vector() {
delete[] _data;
}
};
Nessun commento:
Posta un commento