STL ——list和vector

来源:互联网 发布:淘宝客服培训班 编辑:程序博客网 时间:2024/06/05 16:53

首先简单模拟实现List的普通迭代器

#include<iostream>#include<assert.h>using namespace std;template<class T>struct __ListNode{    T _data;    __ListNode<T> *_prev;    __ListNode<T> *_next;    __ListNode(const T&x)        :_data(x)        , _prev(NULL)        , _next(NULL)    {}};template<class T, class Ref, class Ptr>struct __ListIterator{    typedef __ListNode<T> Node;    typedef __ListIterator<T, Ref, Ptr> Self;    __ListIterator(Node *x)        :_node(x)    {}    // T&  出了作用域,对象还存在所以用Ref    Ref operator*()    {        return _node->_data;    }    //T*   ->返回数据地址    Ptr operator->()    {        return &_node->_data;    }    Self & operator++()    {        _node = _node->_next;        return *this;    }    Self operator++(int)    {        Node* tmp = _node;          _node = _node->_next;        return tmp;    }    Self &operator--()    {        _node = _node->_prev;        return *this;    }    Self operator--(int)    {        Node* tmp = _node;        _node = _node->_prev;        return tmp;    }    bool operator != (const Self& s) const    {        return _node != s._node;    }    bool operator==(const Self &s) const    {        return _node == s._node;    }    Node *_node;};template<class T>class List{public:    typedef __ListNode<T> Node;    typedef __ListIterator<T, T&, T*> Iterator;    List()    {        _head = new Node(T());        _head->_prev = _head;        _head->_next = _head;    }    //析构函数    ~List()    {        Clear();        delete _head;        _head = NULL;    }    Iterator Begin()    {        return Iterator(_head->_next);    }    Iterator End()    {        return Iterator(_head);    }    void Insert(Iterator pos, const T &x)    {        Node *next = pos._node;        Node *prev = next->_prev;        Node *cur = new Node(x);        cur->_prev = prev;        prev->_next = cur;        cur->_next = next;        next->_prev = cur;    }    void Erase(Iterator pos)    {        assert(pos!=End()&&pos._node);        Node* cur = pos._node;        Node* prev = cur->_prev;        Node* next = cur->_next;        prev->_next = next;        next->_prev = prev;        delete cur;    }    bool Emply()    {        return _head == _head->_next;    }    Iterator Find(const T&x)    {        Iterator it = Begin();        while (it != End())        {            if (*it == x)                return it;            it++;        }        return it;    }    void PushBack(const T&x)    {        return Insert(End(), x);    }    void PopBack()    {    return Erase(--End());    }    void PushFront(const T&x)    {        return Insert(Begin(), x);    }    void PopFront()    {        return Erase(Begin());    }    void Clear()    {        Iterator it = Begin();        while (it != End())        {            Node*del = it._node;            ++it;            delete del;        }    }    void Print()    {        Iterator it = Begin();        while (it != End())        {            cout << *it << " ";            cout << endl;            ++it;        }    }protected:    Node *_head;};void PrintList(List<int>& l1)//迭代器 是 类似指针的一个对象  ->   和智能指针相似       //[begin, end) 迭代器是 左闭右开 区间   {    List<int >::Iterator it = l1.Begin();    while (it != l1.End())    {        cout << *it << " ";        it++;    }    cout << endl;}void TestList(){    List<int> l1;    l1.PushBack(1);    l1.PushBack(2);    l1.PushBack(3);    l1.PushBack(4);    l1.Print();    l1.PopBack();    l1.PopBack();    l1.Print();}int main(){    TestList();    system("pause");    return 0;}

接下来实现一下vector

#pragma once#include <iostream>using namespace std;#include <assert.h>template<class T>class Vector{public :    typedef T* Iterator;    typedef const T* ConstIterator;    Vector()        :_start(NULL)        , _finish(NULL)        , _endOfStorage(NULL)    {}    size_t Size()const    {        return _finish - _start;    }    size_t Capacity()const    {        return _endOfStorage - _start;    }    void PushBack(const T& x)    {        Iterator end = End();        Insert(end, x);    }    void PopBack()    {        if (_start)        {            --_finish;        }    }    Iterator Begin()    {        return _start;    }    ConstIterator Begin()const    {        return _start;    }    Iterator End()    {        return _finish;    }    ConstIterator End()const    {        return _finish;    }    //增容    void CheckEndOfStorage()    {        size_t size = Size();        if (_finish == _endOfStorage)        {            //1.开空间 2.赋值 3.释放旧空间 4.赋值            Iterator tmp = new T[2 * size + 3];            for (size_t i = 0; i < size; ++i)            {                tmp[i] = _start[i];            }            delete[] _start;            _start = tmp;            _finish = _start + size;            _endOfStorage = _start + 2 * size + 3;        }    }    //插入(存在迭代器失效问题)    void Insert(Iterator& pos,const T& value)    {        size_t size = pos - _start;        size_t oldSize = Size();        if (pos == End() && _finish < _endOfStorage)//容量足够且尾插        {            _start[oldSize] = value;            ++_finish;        }        else//不是尾插        {            CheckEndOfStorage();            Iterator it = _finish;            pos = _start + size;//重置pos            while (pos != it)            {                *it = *(it - 1);                it--;            }            *pos = value;            ++_finish;        }    }    //将参数给成引用可以解决迭代器失效问题    Iterator Erase(Iterator& pos)    {        Iterator end = End();        Iterator cur = pos;        while (cur != end)        {            *cur = *(cur + 1);            cur++;        }        --_finish;        pos--;        return pos;    }    T& operator[](size_t index)    {        return *(_start + index)    }    const T& operator[](size_t index)const    {        return *(_start + index)    }    //capacity的值在vector中是以1/2来增长的    void Expand(size_t size)    {        size_t capacity = Capacity();        if (size == capacity)        {            capacity = capacity + capacity / 2;            if (capacity < Size() + 1)            {                capacity = Size() + 1;            }        }        _start = new T[capacity];        _finish = _start + size;        _endOfStorage = _start + capacity;    }    bool Empty()    {        return _start == _finish;    }    void Print()    {        size_t size = Size();        for (size_t i = 0; i < size; ++i)        {            cout << _start[i] << " ";        }        cout << endl;    }    //析构函数    ~Vector()    {        if (_start)        {            delete[] _start;            _start = NULL;            _finish = NULL;            _endOfStorage = NULL;        }    }protected :    Iterator _start;    Iterator _finish;    Iterator _endOfStorage;};
原创粉丝点击