bosthhe1 / cpushpush

0 stars 0 forks source link

vector的模拟 #16

Open bosthhe1 opened 1 year ago

bosthhe1 commented 1 year ago
using namespace std;
namespace hxh
{
    template<class T>
    class vector
    {
    public:
        typedef T* iterator;
        typedef const T* const_iterator;
        vector()
            :_start(nullptr)
            , _finish(nullptr)
            , _end_of_storage(nullptr)
        {}
        ~vector()
        {
            delete[] _start;
            _start = _finish = _end_of_storage = nullptr;
        }
        template<class InIterator>//特别注意这里的是使用迭代器区间进行初始化,这里使用泛型拷贝构造,可以兼容更多类型的拷贝构造如(`char *p ="qwertt";vector<string> d1(p,p+5`);这里穿的区间进行构造)
        vector(InIterator begin, InIterator end)
            : _start(nullptr)
            , _finish(nullptr)
            , _end_of_storage(nullptr)
        {
            while (begin != end)
            {
                push_back(*begin);
                begin++;
            }
        }
        vector(const vector<T>& Td)
            :_start(nullptr)
            , _finish(nullptr)
            , _end_of_storage(nullptr)
        {
            vector<T> tmp(Td.begin(), Td.end());
            swap(tmp);
        }
        void swap(vector<T>& Td)
        {
            std::swap(_start, Td._start);
            std::swap(_finish, Td._finish);
            std::swap(_end_of_storage, Td._end_of_storage);
        }
        iterator begin()
        {
            return _start;
        }
        iterator end()
        {
            return _finish;
        }
        const_iterator begin()const
        {
            return _start;
        }
        const_iterator end()const
        {
            return _finish;
        }
        void pop_back()
        {
            assert(_finish > _start);
            _finish--;
        }
        void push_back(const T Ty)
        {
            if (_finish == _end_of_storage)
            {
                reserve(capacity() == 0 ? 4 : capacity() * 2);
            }
            *_finish = Ty;
            ++_finish;
        }
        size_t size()const
        {
            return _finish - _start;
        }
        size_t capacity()const
        {
            return _end_of_storage - _start;
        }
        iterator erase(iterator pos)
        {
            assert(pos < _finish);
            iterator end = pos;
            while (end != _finish)
            {
                *end = *(end+1);
                end++;
            }
            _finish--;
            return pos;
        }
        T operator[](size_t pos)
        {
            return *(_start + pos);
        }
        iterator erase(iterator begin, iterator end)
        {
            assert(begin >= _start);
            assert(_finish > end);
            size_t sz = end-begin;
            iterator it = end;
            while (it != _finish)
            {
                *begin = *it;
                begin++;
                it++;
            }
            _finish -= sz;
            return begin;
        }
        iterator insert(iterator pos, const T& Td)
        {
            assert(pos <= _finish);
            if (_finish == _end_of_storage)
            {
                reserve(capacity() == 0 ? 4 : capacity() * 2);
            }
            iterator end = _finish-1;
            while (end >= pos)
            {
                *(end + 1) = *end;
                end--;
            }
            *pos = Td;
            _finish++;
            return pos;
        }
        void reserve(size_t i)
        {
            assert(capacity()<i);
            size_t sz = size();
            iterator tmp = new T[i];
            if (_start)
            {
                for (size_t i = 0; i < sz; i++)
                {
//这里如果是string类,会去调用他的赋值重载函数,如果用memcpy是按照字节拷贝, 拷贝后他们指向同一块空间,当_start析构 
//函数以后,tmp为野指针,所以不能使用memcpy
                    tmp[i] = _start[i];

                }
                delete[] _start;
            }
            std::swap(tmp, _start);
            _finish = _start + sz;
            _end_of_storage = _start + i;

        }
        void resize(size_t i,const T& Td = T())
        {
            if (i > capacity())
            {
                reserve(i);
                while (_finish != _end_of_storage)
                {
                    push_back(Td);
                }
            }
            else
            {
                if (i > size())
                {
                    iterator end = _start + i;
                    while (_finish != end)
                    {
                        push_back(Td);
                    }
                }
                else
                {
                    _finish = _start + i;
                }
            }
        }
        void clear()
        {
            _finish = _start;
        }
    private:
        iterator _start;
        iterator _finish;
        iterator _end_of_storage;
    };
}
bosthhe1 commented 1 year ago

需要注意的是vector中有很多重复的接口比如operator[]和at(),都可以返回对应下标的值,但是operator[]的底层如果越界是断言,而at是抛异常