Reimplementation of C++ vector I previously attempted to make a C++ vector here, yet it was not very successful. Now I have made a basic reimplementation of it, so I'm checking that it is fine, and that I will not have to re make it again.

Note: I also implemented an allocator class, but it works the same as the std:: equivalent.

template &lt; typename _Ty, typename _Alloc = allocator&lt;_Ty&gt; &gt; class vector
{
public:
    typedef vector&lt;_Ty, _Alloc&gt; _Myt;
    typedef _Ty value_type;
    typedef _Alloc  allocator_type;
    typedef value_type *pointer;
    typedef const value_type *const_pointer;
    typedef value_type *iterator;
    typedef const value_type *const_iterator;
    typedef value_type &amp;reference;
    typedef const value_type &amp;const_reference;
    typedef std::size_t size_type;
    typedef std::ptrdiff_t difference_type;

    vector()
        : __size(0), __capacity(20), __data(_Alloc().allocate(20))
    {
    }

    vector(const _Myt &amp;_Rhs)
        : __size(_Rhs.__size), __capacity(_Rhs.__size + 20), __data(_Alloc().allocate(_Rhs.__size))
    {
        int count = 0;
        for (iterator i = &amp;_Rhs.__data[0]; i != &amp;_Rhs.__data[_Rhs.__size]; ++i, ++count)
        {
            _Alloc().construct(&amp;__data[count], *i);
        }
    }

    ~vector()
    {
        if (!empty())
        {
            for (iterator i = begin(); i != end(); ++i)
            {
                _Alloc().destroy(i);
            }
            _Alloc().deallocate(__data, __capacity);
        }
    }

    _Myt &amp;push_back(const value_type &amp;_Value)
    {
        if (++__size &gt;= __capacity)
        {
            reserve(__capacity * 2);
        }
        _Alloc().construct(&amp;__data[__size - 1], _Value);
        return *this;
    }

    _Myt &amp;push_front(const value_type &amp;_Value)
    {
        if (++__size &gt;= __capacity)
        {
            reserve(__capacity * 2);
        }
        if (!empty())
        {
            iterator _e = end(), _b = begin();
            std::uninitialized_copy(_e - 1, _e, _e);
            ++_e;
            std::copy_backward(_b, _e - 2, _e - 1);
        }
        _Alloc().construct(&amp;__data[0], _Value);
        return *this;
    }

    _Myt &amp;reserve(size_type _Capacity)
    {
        int count = 0;
        if (_Capacity &lt; __capacity)
        {
            return *this;
        }
        pointer buf = _Alloc().allocate(_Capacity);
        for (iterator i = begin(); i != end(); ++i, ++count)
        {
            _Alloc().construct(&amp;buf[count], *i);
        }
        std::swap(__data, buf);
        for (iterator i = &amp;buf[0]; i != &amp;buf[__capacity]; ++i)
        {
            _Alloc().destroy(i);
        }
        _Alloc().deallocate(buf, __capacity);
        __capacity = _Capacity;
        return *this;
    }

    iterator begin()
    {
        return &amp;__data[0];
    }

    iterator end()
    {
        return &amp;__data[__size];
    }

    size_type size()
    {
        return __size;
    }

    size_type capacity()
    {
        return __capacity;
    }

    bool empty()
    {
        return !__data;
    }

    const_pointer data()
    {
        return __data;
    }

private:
    pointer __data;
    size_type __size, __capacity;
};
STOP USING __ its reserved for the implementation.  

Basically stop using underscore until you know the rules.
This code is basically all broken because of this. You code technically is all undefined.
We told you this before. STOP IT.

BUG:

vector(const _Myt &amp;_Rhs)
    : __size(_Rhs.__size), __capacity(_Rhs.__size + 20), __data(_Alloc().allocate(_Rhs.__size))


You are only allocating size space. But your capacity is 20 bytes larger.
Why 20 bytes? Should this not be 20 objects 20 * sizeof(_Ty)

BUG:

You use the allocator construct routines when you are doing placement new. This means the space has not been constructed before. If the location has already got an element in it you must copy over it using the copy constructor.

Thus in

_Myt &amp;push_front(const value_type &amp;_Value)


This line:

   _Alloc().construct(&amp;__data[0], _Value);


You are constructing into location 0 a new object without calling the old objects destructor. If your type is anything non trivial this will screw up any memory management.

BUG

bool empty()
{
    return !__data;  // Both constructors allocate memory.
}                    // So this will never be true.


I think you mean

    return size != 0;


You have a potential leak situation in your reserve().
You provide the strong exception gurantee. BUT if the copy fails (with an exception) then you will leak the buffer. You need to hold the buffer in a smart pointer until you swap so that if there is an exception you gurantee that you deallocate the unused buffer.