vector模拟实现

#include<iostream>
using namespace std;
#include<assert.h>
namespace bit
{
    template<class T>
    class vector
    {
    public:
        // Vector的迭代器是一个原生指针
        typedef T* iterator;
        typedef const T* const_iterator;
         
            iterator begin()
            {
                return _start;
            }
            iterator end()
            {
                return _finish;
            }
            const_iterator cbegin()
            {
                return _start;
            }
            const_iterator cend() const
            {
                return _finish ;
            }
            // construct and destroy
            vector()
            {
                _start = new T[2];
                _finish = _start;
                _endOfStorage = _start + 2;
            }
            vector(int n, const T& value = T())
            {
                _start = new T[n+1];
                int i = 0;
                for (i; i < n; i++)
                {
                    _start[i] = value;
                }
                _finish = _start + i;
                _endOfStorage = _finish;
            }
            template<class InputIterator>
            vector(InputIterator first, InputIterator last)
            {
                int n = last - first + 1;
                _start = new T[n+1];
                for (int i = 0; i < n; i++)
                {
                    _start[i] = first[i];
                }
                _finish = _start + n;
                _endOfStorage = _finish;
            }
            vector(const vector<T>& v)
            {
                int n = v.size();
                _start = new T[n+1];
                for (int i = 0; i < n; i++)
                {
                    _start[i] = v._start[i];
                }
                _finish = _start + n ;
                _endOfStorage = _finish;
            }
            vector<T>& operator= (vector<T> v)
            {
                swap(v);
                return *this;
            }
            ~vector()
            {
                free(_start);
                _start = _finish = _endOfStorage = nullptr;
            }
            // capacity
            size_t size() const
            {
                return _finish - _start ;
            }
            size_t capacity() const
            {
                return _endOfStorage - _start;
            }
            void reserve(size_t n)
            {
                if (n > size())
                {
                    int m = size();
                    int s = size() > 0 ? 2 : 2 * size() * sizeof(T);
                    iterator newn = new T[s];
                    int i = 0;
                    for (i; i < m; i++)
                    {
                        newn[i] = _start[i];
                    }
                    _start = newn;
                    _finish = _start + i ;
                    _endOfStorage = _start + s;
                }
            }
            void resize(size_t n, const T& value = T())
            {
                if (n > size())
                {
                    int m = size();
                    int s = size() > 0 ? 2 : 2 * size() * sizeof(T);
                    iterator newn = new T[s];
                    int i = 0;
                    for (i; i < m; i++)
                    {
                        newn[i] = _start[i];
                    }
                    for (i; i < s; i++)
                    {
                        newn[i] = value;
                    }
                    _start = newn;
                    _finish = _start + n ;
                    _endOfStorage = _start + s;
                }
            }
            ///access///
            T& operator[](size_t pos)
            {
                return _start[pos];
            }
            const T& operator[](size_t pos)const
            {
                return _start[pos];
            }
            ///modify/
            void push_back(const T& x)
            {
                insert(_finish +1, x);
            }
            void pop_back()
            {
                assert(_finish != _start);
                _finish--;
            }
            void swap(iterator a, iterator b)
            {
                iterator tmp = b;
                b = a;
                a = tmp;
            }
            void swap(vector<T>& v)
            {
                swap(_finish, v._finish);
                swap(_start,v._start);
                swap(_endOfStorage,v._endOfStorage);
            }
            iterator insert(iterator pos, const T& x)
            {
                if (_finish >= _endOfStorage)
                {
                    int m = size();
                    int k = pos - _start;
                    int s =  2 * capacity() * sizeof(T);
                    iterator newn = new T[s];
                    int i = 0;
                    for (i; i < m; i++)
                    {
                        newn[i] = _start[i];
                    }
                    _start = newn;
                    _finish = _start + i ;
                    pos = _start + k;
                    _endOfStorage = _start + s;
                }
                if (pos > _finish)
                {
                    *_finish = x;
                }
                else
                {
                    iterator t = _finish ;
                    while (t > pos)
                    {
                        *t = *(t - 1);
                        t--;
                    }
                    *t = x;
                }
                _finish++;
                return pos;
            }
            iterator erase(iterator pos)
            {
                iterator k = pos;
                while (k != _finish)
                {
                    *k = *(k + 1);
                    k++;
                }
                _finish--;
                return pos;
            }
    private:
        iterator _start; // 指向数据块的开始
        iterator _finish; // 指向有效数据的尾
        iterator _endOfStorage; // 指向存储容量的尾
    };
  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值