还是要注意深浅拷贝的问题,尤其是当T=string 等类型时,使用memcpy会进行字节拷贝,也就是浅拷贝,所以我们要特别注意。

#include<iostream>
using namespace std;

namespace sir
{
    template<class T>
    class vector
    {
        
    public:
        typedef T* iterator;
        vector()
            :_start(nullptr)
            , _finish(nullptr)
            , _endofstorage(nullptr)
        {}
        // ============ 补充拷贝构造函数(核心修复!) ============
        vector(const vector<T>& v)
            :_start(nullptr)
            , _finish(nullptr)
            , _endofstorage(nullptr)
        {
            size_t sz = v.size();
            size_t cap = v.capacity();
            if (cap > 0)
            {
                T* tmp = new T[cap];
                for (size_t i = 0; i < sz; ++i)
                {
                    tmp[i] = v._start[i];
                }
                _start = tmp;
                _finish = _start + sz;
                _endofstorage = _start + cap;
            }
        }
        ~vector()
        {
            delete[] _start;
        _start = _finish = _endofstorage = nullptr;
        }
        
        iterator begin()
        {
            return _start;
        }
        iterator end()
        {
            return _finish;
        }
        size_t size() const
        {
            return _finish - _start;
        }
        size_t capacity() const
        {
            return _endofstorage - _start;
        }
        T& operator[](size_t pos)
        {
            return _start[pos];
        }
        const T& operator[](size_t pos)const
        {
            return _start[pos];
        }
        //扩容
        void reserve(size_t n)
        {
            size_t sz = size();
            
            if (n > capacity())
            {
                T* tmp = new T[n];
                if (_start)
                {
                    //memcpy(tmp, _start, sizeof(T) * size());
                    //delete[] _start;
                    for (size_t i = 0; i < sz; i++)
                    {
                        tmp[i] = _start[i];

                    }
                    delete[] _start;
                }
                _start = tmp;
                _finish = tmp + sz;
                _endofstorage = tmp + n;
            }
            
        }
        //尾插
        void push_back(const T& x)
        {
            if (_finish == _endofstorage)
            {
                size_t newcapacity = capacity() == 0 ? 2 : capacity() * 2;
                reserve(newcapacity);
            }
            *_finish = x;
            _finish++;
        }
        //尾删
        void pop_back()
        {
            --_finish;
        }
        //删除任意位置,返回当前位置的下一个位置
        T* erase(T* pos)
        {
            T* it = pos;
            while (it < _finish)
            {
                *it = *(it + 1);
                ++it;
            }
            --_finish;
            return pos;
        }
        //插入
        T* insert(T* pos, const T&x)
        {
            if (pos <= _endofstorage)
            {
                if (_finish == _endofstorage)
                {
                    size_t n= pos - _start;
                    size_t newcapacity = capacity() == 0 ? 2 : capacity() * 2;
                    reserve(newcapacity);
                    //扩容之后原来的pos就失效了,需要重新计算pos的位置
                    pos = _start + n;
                }
                T* end = _finish - 1;
                while (end >= pos)
                {
                    *(end + 1) = *end;
                    end--;
                }
                *pos = x;
                ++_finish;
            }
            return pos;
        }
    private:
        T* _start;
        T* _finish;
        T* _endofstorage;

    };
}
  
int main()
{
    sir::vector<char> v1;
    v1.push_back('a');
    v1.push_back('b');
    v1.push_back('c');
    v1.push_back('d');
    v1.push_back('e');
    v1.pop_back();
    v1.erase(v1.begin()+2);
    for (auto num : v1) 
    { // 底层调用 begin()/end()
        cout << num << " ";
    }
    cout << endl;
    for (size_t i = 0; i < v1.size(); i++)
    {
        cout << v1[i];
    }
    cout << endl;
    sir::vector<char>::iterator it = v1.begin();
    while (it != v1.end())
    {
        cout<<*it<<" ";
        it++;
    }
    cout << endl;

    it = v1.begin();
    v1.insert(it + 2, 'c');
    while (it != v1.end())
    {
        cout << *it << " ";
        it++;
    }
    cout << endl;
    sir::vector<char> v2 = v1;
    sir::vector<char>::iterator itt = v2.begin();
    while (itt != v2.end())
    {
        cout << *itt << " ";
        itt++;
    }
    cout << endl;

    return 0;
}

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐