学无先后,达者为师

网站首页 编程语言 正文

模拟实现vector

作者:DayDay upup 更新时间: 2022-09-22 编程语言
namespace zjq {

    template<class T>
    class vector
    {
    public:
        typedef T* iterator;
        typedef const T* const_iterator;

        iterator begin() 
        {
            return _start;
        }

        iterator end() 
        {
            return _finish;
        }

        const_iterator cbegin() const
        {
            return _start;
        }

        const_iterator cend() const
        {
            return _finish;
        }

        size_t size()const
        {
            return _finish - _start;
        }

        size_t capacity()const
        {
            return _endofstorage - _start;
        }

        void reserve(size_t n)
        {
            size_t sz = size();
            if (capacity() < n)
            {
                iterator tmp = new T[n];
                for (size_t i = 0; i < sz; i++)
                {
                    tmp[i] = _start[i];
                }
                if (_start)
                {
                    delete[] _start;
                }
                _start = tmp;
                _finish = _start + sz;
                _endofstorage = _start + n;
            }
        }

        void resize(size_t n,const T& val =T())
        {
            if (n > capacity())
            {
                reserve(n);
            }

            if (size() >= n)
            {
                _finish = _start + n;
            }
            else
            {
                while (_finish < _endofstorage)
                {
                    *_finish = val;
                    ++_finish;
                }
            }


        }

        vector()
            :_start(nullptr)
            ,_finish(nullptr)
            ,_endofstorage(nullptr)
        {
        }
        vector(size_t n, const T& val = T())
            : _start(nullptr)
            , _finish(nullptr)
            , _endofstorage(nullptr)

        {
            reserve(n);
            for (size_t i = 0; i < n; i++)
            {
                push_back(val);
            }
        }

        vector(int n, const T& val = T())
            : _start(nullptr)
            , _finish(nullptr)
            , _endofstorage(nullptr)

        {
            reserve(n);
            for (int i = 0; i < n; i++)
            {
                push_back(val);
            }
        }

        template<class InputIterator>
        vector(InputIterator first, InputIterator last)
            : _start(nullptr)
            , _finish(nullptr)
            , _endofstorage(nullptr)
        {
            while (first != last)
            {
                push_back(*first);
                ++first;
            }

        }


        vector(const vector<T>& v)
            :_start(nullptr)
            , _finish(nullptr)
            , _endofstorage(nullptr)
        {
            vector tmp(v.begin(), v.end());
            swap(tmp, *this);
        }

        ~vector()
        {
            delete[] _start;
            _start = _finish = _endofstorage = nullptr;
        }

        T& operator[](size_t pos)
        {
            return _start[pos];
        }

        const T& operator[](size_t pos) const
        {
            return _start[pos];
        }


        void push_back(const T& x)
        {
            if (_finish == _endofstorage)
            {
                size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
                reserve(newcapacity);
            }
            *_finish = x;
            ++_finish;
        }

        void pop_back()
        {
            assert(size() > 0);
            --_finish;
        }

        iterator insert(iterator pos, const T& val = T())
        {
            assert(pos <= end());
            if (size() == capacity())
            {
                size_t i = pos - _start;
                size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
                reserve(newcapacity);
                pos = _start + i;
            }
            iterator _end = _finish;
            while (_end != pos)
            {
                *_end = *(_end - 1);
                --_end;
            }
            *pos = val;
            ++_finish;
            return pos;
        }

        iterator erase(iterator pos)
        {
            assert(pos <= end());
            iterator _end = pos;
            while (_end != _finish)
            {
                *_end = *(_end + 1);
                ++_end;
            }
            --_finish;
            return pos;
        }
        void swap(vector<T>& v)
        {
            std::swap(_start, v._start);
            std::swap(_finish, v._finish);
            std::swap(_endofstorage, v._endofstorage);
        }


    private:
        iterator _start;
        iterator _finish;
        iterator _endofstorage;
    };
}

原文链接:https://blog.csdn.net/zjq_love/article/details/125764844

栏目分类
最近更新