Skip to content

Instantly share code, notes, and snippets.

@webmonch
Created November 19, 2014 21:52

Revisions

  1. webmonch created this gist Nov 19, 2014.
    75 changes: 75 additions & 0 deletions gistfile1.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    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;
    };