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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值