C++实现Vector->类型萃取和List->迭代器

来源:互联网 发布:网速限制软件 编辑:程序博客网 时间:2024/06/07 06:10

容器

1.Vector(也可以称之为顺序表)

#include <iostream>using namespace std;#include <assert.h>template <class T>class Vector{public:    Vector()//构造函数        :_data(NULL)        , _size(0)        , _capa(0)    {    }    ~Vector()//析构函数    {        if (_data)        {            delete[] _data;            _data = NULL;            _size = 0;            _capa = 0;        }    }    Vector(const Vector<T>& s)//拷贝构造    {        _data = new T[s._size];        for (size_t i = 0; i < s._size; i++)//效率低,但是不会出错        {            _data[i] = s._data[i];        }        //memcpy(_data, s._data, s._size*sizeof(T));//效率高,但是会出错        _size = _capa = s._size;    }    void Swap(Vector<T>& v)    {        std::swap(_data, v._data);        std::swap(_capa, v._capa);        std::swap(_size, v._size);    }    Vector<T>& operator=(const Vector<T>& s)//赋值运算符“=”重载    {        if (this != &s)        {            Vector<T> tmp(s);            Swap(tmp);        }        return *this;    }    void PushBack(const T& x)//尾插    {        CheckCapcacity();        _data[_size++] = x;    }    void PopBack()//尾删    {        assert(_size > 0);        --_size;    }    void PushFront(T x)//头插    {        CheckCapcacity();        T end = _size - 1;        for (size_t i = 0; i < _size; i++)        {            _data[end + 1] = _data[end];            end--;        }        _data[0] = x;        _size++;    }    void PopFront()//头删    {        for (size_t i = 0; i < _size; i++)        {            _data[i] = _data[i + 1];        }        _size--;    }    void Insert(size_t pos, const T& x)//指定位置插入    {        assert(pos < _size);        CheckCapcacity();        T end = _size - 1;        for (size_t i = pos - 1; i < _size; i++)        {            _data[end + 1] = _data[end];            end--;        }        _data[pos] = x;        _size++;    }    void Erase(size_t pos)//指定位置删除    {        assert(pos < _size);        for (size_t i = pos; i < _size; i++)        {            _data[i] = _data[i + 1];        }        _size--;    }    void Assign(size_t n, const T& s)//赋n个值为x的元素到vector容器中,        //这个容器会清除掉vector容器中以前的内容。    {        Vector<T> v(s._data);        Swap(tmp);    }    void clear()    {        _size = 0;    }    T& operator[](size_t pos)//[]重载,可读可写    {        assert(pos < _size);        return _data[pos];    }    T& operator[](size_t pos)const//只读    {        assert(pos < _size);        return _data[pos];    }    const T& Front()const    {        return _data[0];    }    const T& Back()const    {        return _data[_size - 1];    }    size_t Size()const    {        return _size;    }    size_t Capacity()const    {        return _capa;    }    bool Empty()const    {        return _size == 0;    }}    void CheckCapcacity()    {        if (_size == _capa)        {            size_t newCapacity = _capa * 2 + 3;            T* pData = new T[newCapacity];            //memcpy(pData, _data, sizeof(T)*_size);            for (size_t idx = 0; idx < _size; ++idx)            {                pData[idx] = _data[idx];            }            delete[] _data;            _data = pData;            _capa = newCapacity;        }    }    void Print()//输出    {        for (size_t i = 0; i < _size; ++i)        {            cout << _data[i] << " ";        }        cout << endl;    }private:    T *_data;    size_t _size;    size_t _capa;};void Test(){    Vector<int> s;    s.PushBack(1);    s.PushBack(2);    s.PushBack(3);    s.PushBack(4);    s.Print();    s.PopBack();    s.Print();    s.PushFront(5);    s.Print();    s.PopFront();    s.Print();    s.Insert(1, 8);    s.Print();    s.Erase(1);    s.Print();    Vector<int> s1(s);    s1.Print();    Vector<int> s2;    s2 = s1;    s2.Print();}int main(){    Test();    system("pause");}

2.List(双向带头循环链表)

#include <iostream>using namespace std;#include <assert.h>template<class T>struct ListNode{    ListNode(const T& data = T())    : _pPre(NULL)    , _pNext(NULL)    , _data(data)    {}    ListNode<T>* _pPre;    ListNode<T>* _pNext;    T            _data;};//迭代器template<class T, class Ref>class ListIterator{    typedef ListIterator<T, Ref> Self;public:    ListIterator()        : _pNode(NULL)    {}    ListIterator(ListNode<T>* pNode)        : _pNode(pNode)    {}    Self& operator++()    {        _pNode = _pNode->_pNext;        return *this;    }    Self operator++(int)    {        Self temp(*this);        _pNode = _pNode->_pNext;        return temp;    }    Self& operator--()    {        _pNode = _pNode->_pPre;        return *this;    }    Self operator--(int)    {        Self temp(*this);        _pNode = _pNode->_pPre;        return temp;    }    bool operator==(const Self& s)    {        return _pNode == s._pNode;    }    bool operator!=(const Self& s)    {        return _pNode != s._pNode;    }    Ref operator*()    {        return _pNode->_data;    }private:    ListNode<T>* _pNode;};void FunTest(){    List<int> L;    L.PushBack(1);    L.PushBack(2);    L.PushBack(3);    L.PushBack(4);    List<int>::Iterator it = L.Begin();    while (it != L.End())    {        cout << *it << "->";        ++it;    }    cout << endl;    L.Print();    L.PushFront(0);    L.Print();    L.PopBack();    L.Print();    L.PopFront();    L.Print();    ListNode<int>* pos = L.Find(2);    L.Insert(pos, 5);    L.Print();    L.Erase(pos);    L.Print();}template<class T>class List{public:    typedef ListIterator<T, T&> Iterator;public:    Iterator Begin()    {        return Iterator(_pHead->_pNext);    }    Iterator End()    {        return Iterator(_pHead);    }    List()    {        CreateHead();    }    List(size_t n, const T& data)    {        CreateHead();        for (size_t idx = 0; idx < n; ++idx)            PushBack(data);    }    ~List()    {        Clear();        delete _pHead;        _pHead = NULL;    }    void PushBack(const T& data)    {        ListNode<T>* pTailNode = _pHead->_pPre;        ListNode<T>* pNewNode = new ListNode<T>(data);        pTailNode->_pNext = pNewNode;        pNewNode->_pNext = _pHead;        pNewNode->_pPre = pTailNode;        _pHead->_pPre = pNewNode;    }    void PopBack()    {        if (_pHead == _pHead->_pNext)        {            assert(false);            return;        }        ListNode<T>* pTailNode = _pHead->_pPre;        pTailNode->_pPre->_pNext = _pHead;        _pHead->_pPre = pTailNode->_pPre;        delete pTailNode;    }    void PushFront(const T& data)    {        ListNode<T>* pFirst = _pHead->_pNext;        ListNode<T>* pNewNode = new ListNode<T>(data);        pNewNode->_pNext = pFirst;        pFirst->_pPre = pNewNode;        pNewNode->_pPre = _pHead;        _pHead->_pNext = pNewNode;    }    void PopFront()    {        ListNode<T>* pFirst = _pHead->_pNext;        _pHead->_pNext = pFirst->_pNext;        pFirst->_pNext->_pPre = _pHead;        delete pFirst;    }    Iterator Find(const T& data)    {        ListNode<T>* pCur = _pHead->_pNext;        while (pCur != _pHead)        {            if (data == pCur->_data)                return Iterator(pCur);            pCur = pCur->_pNext;        }        return Iterator();    }    void Insert(ListNode<T>* pos, const T& data)    {        ListNode<T>* pNewNode = new ListNode<T>(data);        pNewNode->_pNext = pos;        pos->_pPre->_pNext = pNewNode;        pNewNode->_pPre = pos->_pPre;        pos->_pPre = pNewNode;    }    void Erase(ListNode<T>* pos)    {        pos->_pPre->_pNext = pos->_pNext;        pos->_pNext->_pPre = pos->_pPre;        delete pos;    }    void Clear()    {        ListNode<T>* pCur = _pHead->_pNext;        ListNode<T>* pPre = NULL;        while (pCur != _pHead)        {            pPre = pCur;            pCur = pCur->_pNext;            delete pPre;        }        _pHead->_pNext = _pHead;        _pHead->_pPre = _pHead;    }    bool Empty()const    {        return _pHead->_pNext == _pHead;    }    size_t Size()const    {        size_t count = 0;        ListNode<T>* pCur = _pHead->_pNext;        while (pCur != _pHead)        {            count++;            pCur = pCur->_pNext;        }        return count;    }    void Print()    {        ListNode<T>* pCur = _pHead->_pNext;        while (pCur != _pHead)        {            cout << pCur->_data << "->";            pCur = pCur->_pNext;        }        cout << "NULL" << endl;    }private:    void CreateHead()    {        _pHead = new ListNode<T>;        _pHead->_pNext = _pHead;        _pHead->_pPre = _pHead;    }private:    ListNode<T>* _pHead;};

模板类的全特化和偏特化

template<class T>class Vector{public:    Vector()    {        cout<<typeid(T).name()<<endl;    }private:    T* _pData;    size_t _size;    size_t _capacity;};// 全特化template<>class Vector<int>{public:    Vector<int>()    {        cout<<"int"<<endl;    }private:    int* _pData;    size_t _size;    size_t _capacity;};// 偏特化: template<class T>class Vector<T*>{public:    Vector()    {        cout<<typeid(T).name()<<"*"<<endl;    }};//偏特化template<class T>class Vector<T&>{public:    Vector()    {        cout<<typeid(T).name()<<"&"<<endl;    }};int main(){    Vector<char> v1;    Vector<int> v2;    Vector<int*> v3;    Vector<char*> v4;    Vector<int&> v5;    return 0;}

类型萃取

// 内置类型struct _TrueType_{    static bool Get()    {        return true;    }};// 自定义类型struct _FalseType_{    static bool Get()    {        return false;    }};// 模板类的特化-->任意类型template<class T>struct TypeTraits{    typedef _FalseType_ IsPOD; // plain old type--->内置类型};// 特化template<>struct TypeTraits<int>{    typedef _TrueType_ IsPOD;};template<>struct TypeTraits<char>{    typedef _TrueType_ IsPOD;};template<class T>void _Copy(T* dst, T* src, size_t n, _FalseType_){    for (size_t idx = 0; idx < n; ++idx)    {        dst[idx] = src[idx];    }}template<class T>void _Copy(T* dst, T* src, size_t n, _TrueType_){    memcpy(dst, src, sizeof(T)*n);}template<class T>void Copy(T* dst, T* src, size_t n){    _Copy(dst, src, n, TypeTraits<T>::IsPOD());}#include <string>void TestCopy(){    int array1[] = {1,2,3,4,5,6,7,8,9};    int array2[sizeof(array1)/sizeof(array1[0])];    // 内置类型    Copy(array2, array1, sizeof(array1)/sizeof(array1[0]));    string s1[] = {"1111", "2222", "3333", "4444"};    string s2[sizeof(s1)/sizeof(s1[0])];    Copy(s2, s1, sizeof(s1)/sizeof(s1[0]));}int main(){    TestCopy();    return 0;}
原创粉丝点击