vector和list的使用

vector和list的區別
1.在存儲中 vector是連續存儲的;list中的對象不一定是連續存儲的。
2.list如果要隨機訪問某個元素需要遍歷list,沒有效率。vector重載了[ ]可以直接訪問。
3.在list中插入元素,尤其是在首尾插入元素,效率很高,只需要改變元素的指針,但是vector要隨機插入或刪除數據需要前移或者後移,內存塊的拷貝,另外,當該數組後的內存空間不夠時,需要重新申請一塊足夠大的內存並進行內存的拷貝,開銷比較大。
4.list是雙向的,而vector是單向的。
建議:如果是是大量的插入和刪除應使用list,少量的數據如果進行插入和刪除用vector。
MyVector的實現

template<class T>

class Vector
{
public:
    typedef T* Iterator;
    //typedef const T* Iterator;

    Vector()
        :_start(NULL)
        ,_finish(NULL)
        ,_endOfStorage(NULL)
    {}
    ~Vector()
    {
        if (_start)
        {
            delete[] _start;
            _start = NULL;
            _finish = NULL;
            _endOfStorage = NULL;
        }
    }

    Vector(int n, const T& data = T())
        :_start(NULL)
        , _finish(NULL)
        , _endOfStorage(NULL)
    {
        _start = new T[n];
        for (size_t i = 0; i < n; ++i)
        {
            _start[i] = data;
        }
        _finish = _start + n;
        _endOfStorage = _finish;
    }

    void PushBack(const T& x)
    {
        Iterator end=End();
        Insert(end,x);
    }

    Vector(const Vector<T>& v)
        :_start(0)
        , _finish(0)
        , _endOfStorage(0)
    {
        //可以分爲兩種情況:①容量等於size(stl)  ②容量大於size
        size_t size = v.Size();
        _start = new T[size];
        for (size_t i = 0; i < size; ++i)
        {
            _start[i] = v._start[i];
         }
        _finish = _start + v.Size();
        _endOfStorage = _finish;
    }

    void PopBack()
    {
        if(_start)
        {
            --_finish;
        }
    }

    void PopFront()
    {
        if(_start)
        {
            Erase(Begin());
        }
    }

    Iterator Erase(Iterator pos)
    {
        assert(pos<=End() && pos);
        Iterator end=End();
        Iterator cur=pos;
        while(cur!=end)
        {
            *cur=*(cur+1);
            cur++;
        }
        --_finish;
        --pos;
        return pos;
    }

    void Insert(Iterator& pos,const T& x)
    {
        size_t n=pos-_start;
        if(_finish==_endOfStorage)
        {
            size_t len=Capacity()==0 ? 3:Capacity()*2;
            Expand(len);
        }
        pos=_start+n;
        for(Iterator end=End();end!=pos;--end)
        {
            *end=*(end-1);
        }
        *pos=x;
        ++_finish;
    }

    Iterator End()
    {
        return _finish;
    }

    Iterator Begin()
    {
        return _start;
    }

    inline size_t Size()
    {
        return _finish-_start;
    }

    inline size_t Capacity()
    {
        return _endOfStorage-_start;
    }

    void Expand(size_t n)//增容
    {
        const size_t size=Size();
        const size_t capacity=Capacity();
        if(n>capacity)
        {
            T* tmp=new T[n];
            for(size_t i=0;i<size;i++)
            {
                tmp[i]=_start[i];
            }
            delete _start;
            _start=tmp;
            _finish=_start+size;
            _endOfStorage=_start+n;
        }
    }

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

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


protected:
    Iterator _start;
    Iterator _finish;
    Iterator _endOfStorage;
};


void test1()
{
    Vector<int> v;
    v.PushBack(1);
    v.PushBack(2);
    v.PushBack(3);
    v.PushBack(3);
    v.PushBack(4);



    Vector<int>::Iterator it=v.Begin();
    while(it!=v.End())
    {
        cout<<*it<<" ";
        ++it;
    }
    cout<<endl;

    Vector<int>::Iterator pos=v.Begin();
    //v.PopFront();
    //v.PopBack();
    v.Erase(pos+2);
    Vector<int>::Iterator it1=v.Begin();//迭代器實現
    while(it1!=v.End())
    {
        cout<<*it1<<" ";
        ++it1;
    }
    cout<<endl;
}

MyList

#include <iostream>
using namespace std;
template<class T>

struct ListNode   //--------雙向鏈表節點
{
    ListNode(const T& data = 0)
        :_next(NULL)
        , _prev(NULL)
        ,_data(data)
    {}

    T _data;
    ListNode<T>* _next;
    ListNode<T>* _prev;
};

template<class T, class Ref, class Ptr>
struct _Iterator//迭代器的重載
{
    typedef ListNode<T> Node;
    typedef _Iterator<T, Ref, Ptr> Self;
    _Iterator(Node* node)
        :_node(node)
    {}

    Ref operator*()//  重載解引用
    {
        return _node->_data;
    }

    Self& operator++()//   ++重載 
    {
        _node = _node->_next;
        return *this;
    }

    bool operator!=(const Self& other)const// !=重載
    {
        return other._node != _node;
    }

    Node* _node;
};

template<class T>
class List
{
    typedef ListNode<T> Node;
public:
    typedef  _Iterator<T, T&, T*>  Iterator;
    typedef _Iterator<T, const T&, const T*> ConstIterator;

    List()    
        :_head(NewNode())
    {
        _head->_next = _head;
        _head->_prev = _head;
    }

    ~List()   
    {
        Release();
        delete _head;
    }

    Node* NewNode(const T& x)// 傳值
    {
        return new Node(x);
    }

    Node* NewNode()// 用於head傳值
    {
        return new Node();
    }

    void PushBack(const T& x)   
    {
        Node* tail = _head->_prev;
        Node* tmp = NewNode(x);
        tail->_next = tmp;
        tmp->_prev = tail;
        tmp->_next = _head;
        _head->_prev = tmp;
    }


    Iterator Begin()    //定義返回迭代器類型的Begin()和End()  
    {
        return Iterator(_head->_next);
    }

    Iterator End()
    {
        return Iterator(_head);
    }

    ConstIterator Begin()const   
    {
        return ConstIterator(_head->_next);
    }

    ConstIterator End()const
    {
        return ConstIterator(_head);
    }

    void Release() // 釋放
    {
        Node* tmp = _head->_next;
        while (tmp != _head)
        {
            Node* del = tmp;
            tmp = tmp->_next;
            delete del;
        }
    }

    Iterator Erase(Iterator it)// 刪除
    {
        Node* prev = it._node->_prev;
        Node* next = it._node->_next;
        delete it._node;
        prev->_next = next;
        next->_prev = prev;
        return  prev;
    }

    void PopBack()
    {
        Erase(_head->_prev);
    }

    void Remove(int pos)// 指定位置刪除
    {
        Iterator it = Begin();
        while (it != End())
        {
            if (pos == 1)
            it = Erase(it);
            ++it;
            --pos;
        }
    }
protected:
    Node* _head;
};


void test2()
{
    List<int> l;  
    for (int i = 0; i < 5; i++)
    {
        l.PushBack(i);
    }
    //l.PopBack();

    List<int>::Iterator it=l.Begin();
    while(it!=l.End())
    {
        cout<<*it<<" ";
        ++it;
    }
    cout<<endl;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章