vector和list的使用

vector

是C++标准模板库中的部分内容,是一种容器。它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库。vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vector是一个能够存放任意类型的动态数组,能够增加和压缩数据。
使用它时需要包含头文件:#include
vector类型的初始化:
vector a ; //声明一个int型向量a;
vectora(10); //声明一个大小为是10的向量a;
vector a(10, 1) ; //声明一个初始大小为10,初始值都为1的向量;
int b[7]={1,2,3,4,5,9,8};vector a(b,b+7); //从数组中获得初值
vector的基本操作:
这里写图片描述
这里写图片描述
vector的基本实现

//构造函数
Vector()
    :_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;
}

//拷贝构造函数
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];
    }*/
    __Copy<T>(_start, v._start, size);
    _finish = _start + v.Size();
    _endOfStorage = _finish;
}

size_t Size()const

{
    return _finish - _start;
}

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

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)
   {
        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)
}


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;
    }
}

list

list 的特色是在集合的任何位置增加或删除元素都很快,但是不支持随机存取,list 以模板方式实现(即泛型),可以处理任意型别的变量,包括使用者自定义的资料型态例如:它可以是一个放置整数(int)型态的 list、也可以是放置字串(char 或 string)型态的 list、或者放置使用者自定类别(user-defined class)的 list,但是随机访问却比较慢。
常用的操作:
这里写图片描述
这里写图片描述
基本实现:

template<class T>  
struct __ListNode  
{  
     T  _data;  
     __ListNode<T>* _next;  
     __ListNode<T>* _prev;  
      __ListNode(const T& x)  
          :_data(x)  
          ,_next(NULL)  
          ,_prev(NULL)  
      {  
      }  
};  
template <class T,class Ref,class Ptr >  
struct __ListIterator  
{  
    typedef  __ListNode<T>   Node;  
    typedef  __ListIterator<T,Ref,Ptr> Self;  
__ListIterator(Node* node)     
        :_node(node)  
    {  
    }  
    Ref  operator*()  
    {  
        return _node->_data;  
    }  
    Ptr  operator->()  
    {  
        return &(_node->_data)  
    }  
    Self& operator++()  
    {  
       _node=_node->_next;  
       return *this;  
    }  
    Self& operator--()  
    {  
        _node=_node->_prev;  
        return *this;  
    }  
    Self operator++(int)  
    {  
        Self tmp=_node;  
        _node=_node->_next;  
        //return tmp;  
        return Self(tmp)     
    }  
    Self  operator--(int)  
    {     
        Self tmp=(*this);  
        _node=_node->_prev;  
        return tmp;  
    }  
    bool operator!=(const Self& s) const  
    {  
     return this->_node!=s._node;  
    }  
    bool operator==(const Self& s) const  
    {  
        return this->_node==s._node;  
    }  
    Node* _node;  
};  
template<class T>  
struct List  
{  
    typedef  __ListNode<T> Node;  
public:  
    typedef  __ListIterator<T,T&,T*> Iterator;  
    typedef  __ListIterator<T,const T&,const T*> ConstIterator;  
    Node* GetNode(const T& x)  
    {  
        return new Node(x);  
    }  
    List()  
    {  
       _head=GetNode(T());  
       _head->_next=_head;  
       _head->_prev=_head;  
    }  
    Iterator Begin()  
    {  
      return Iterator(_head->_next);  
    }  
    Iterator End()  
    {  
      return Iterator(_head);  
    }  
    ConstIterator Begin() const  
    {  
        return ConstIterator(_head->_next);  
    }  
    ConstIterator End() const  
    {  
        return ConstIterator(_head);  
    }  

    void PushBack(const T& x)  
    {  
      /*  Node* _tail=_head->_prev;  
        Node* tmp=GetNode(x);  
        _tail->_next=tmp;  
        tmp->_prev=_tail;  
        tmp->_next=_head;  
        _head->_prev=tmp;*/  
        Insert(End(),x);  
    }  
  void PopBack()  
  {  
     /* assert(_head->_prev );  
      Node* tail=_head->_prev;  
      Node* prev=tail->_prev;  
      Node* next=tail->_next;  
      prev->_next=next;  
      next->_prev=prev;  
      delete tail;*/  
      Erase(--End());  
  }  
  void PushFront(const T& x)  
  {  
    /*assert(_head)  
      Node* tmp=GetNode(x);  
      Node* next=_head->_next;  

      _head->_next=tmp;  
      tmp->_prev=_head;  

      tmp->_next=next;  
       next->_prev=tmp;*/  
      Insert(Begin(),x);  
  }  
  void PopFront()  
  {  
      /*assert(_head->_next);  
     Node* tmp=_head->_next;  
     Node* next=tmp->_next;  

     _head->_next= next;  
     next->_prev=_head;  
     delete tmp;*/  

       Erase(Begin());  
  }  
 Iterator Insert(Iterator pos, const T& x)  
 {  
     assert(pos._node);  
     Node* tmp=GetNode(x);  
     Node* cur=pos._node;  
     Node* prev=cur->_prev;  

     prev->_next=tmp;  
     tmp->_prev=prev;  
     tmp->_next=cur;  
     cur->_prev=tmp;  
     return  tmp;  
 }  
 Iterator  Erase(Iterator pos)  
{  
assert(pos._node && pos._node!=NULL);  
Node* tmp=pos._node;  
Node* next=tmp->_next;  
Node* prev=tmp->_prev;  

next->_prev=prev;  
prev->_next=next;  

delete tmp;  
return Iterator(next);  
}  

protected:  
    Node* _head;  
};  
void PrintList(const List<int>& l)  
{  
    List<int>::ConstIterator It=l.Begin();  
    while(It!=l.End())  

    {   
       cout<<*It<<" ";  
       ++It;  
    }  
    cout<<endl;
    }  

猜你喜欢

转载自blog.csdn.net/qqkb1016/article/details/78447087