vector模拟实现

vector模拟实现_第1张图片

vector模拟实现_第2张图片

vector模拟实现_第3张图片

代码

#pragma once

#include 


template
class vector
{

public:

    // Vector的迭代器是一个原生指针

    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;
    }



    // construct and destroy

    vector()
    {

    }

    vector(int n, const T& value = T())
    {
        reserve(n);
        for (int i = 0; i < n; i++)
        {
            push_back(value);
        }
    }

        template
        vector(InputIterator first, InputIterator last)
        {
            while (first != last)
            {
                push_back(*first);
                ++first;
            }

        }

    vector(const vector& v)
    {
        reserve(v.capacity());

        for (int i = 0; i < v.size(); i++)
        {
            push_back(v[i]);
        }
    }

    vector& operator= (vector tmp)
    {
        swap(tmp);

        return *this;
    }

    ~vector()
    {
        delete[]_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 > capacity())
        {
            T* tmp = new T[n];
            size_t sz = size();
            
            if (_start)
            {
                for (size_t i = 0; i < sz; i++)
                {
                    tmp[i] = _start[i];
                }
                delete[]_start;
            }

            _start = tmp;
            _finish = _start + sz;
            _endOfStorage = _start + n;
        }
    }

    void resize(size_t n, const T& value = T())
    {
        if (n < size())
        {
            _finish = _start + n;
        }
        else
        {
            reserve(n);
            while (_finish < _start + n)
            {
                *_finish = value;
                ++_finish;
            }
        }
    }



        ///access///

    T& operator[](size_t pos)
    {
        assert(pos < size());

        return _start[pos];
    }

    const T& operator[](size_t pos)const
    {
        assert(pos < size());

        return _start[pos];
    }



        ///modify/

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

        *_finish = x;
        _finish++;
    }

    void pop_back()
    {
        assert(size()==0);

        _finish--;
    }

    void swap(vector& v)
    {
        std::swap(_start, v._start);
        std::swap(_finish, v._finish);
        std::swap(_endOfStorage, v._endOfStorage);
    }

    iterator insert(iterator pos, const T& x)
    {
        assert(pos >= 0);
        assert(pos <= _finish);

        if (_finish == _endOfStorage)
        {
            size_t len = pos - _start;
            reserve(capacity() == 0 ? 4 : 2 * capacity());
            pos = _start + len;
        }
        iterator end = _finish;
        while (end > pos)
        {
            _start[end] = _start[end - 1];
            --end;
        }
        _start[pos] = x;
        _finish++;

        return pos;
    }

    iterator erase(iterator pos)
    {
        assert(pos >= 0);
        assert(pos < _finish);

        iterator it = pos + 1;
        while (it < _finish)
        {
            _start[it - 1] = _start[it];
            ++it;
        }
        _finish--;

        return pos;
    }

private:

    iterator _start = nullptr; // 指向数据块的开始

    iterator _finish = nullptr; // 指向有效数据的尾

    iterator _endOfStorage = nullptr; // 指向存储容量的尾
};

你可能感兴趣的:(算法)